Skip to content
AgileLogics
4 min read

Unit Testing in C++

Prerequisites: Build tools: CMake for C++

Catch2 and Google Test are the two most widely used C++ testing frameworks — both integrate directly with CMake.

#include <catch2/catch_test_macros.hpp>

int add(int a, int b) { return a + b; }

TEST_CASE("add sums two numbers") {
    REQUIRE(add(2, 3) == 5);
}

TEST_CASE("add handles negative numbers") {
    REQUIRE(add(-1, 1) == 0);
}
cmake --build .
ctest

Combined with a sanitizer-enabled build (-fsanitize=address,undefined, as covered for C), a C++ test suite catches both incorrect logic and memory-safety bugs in the same run.