Catch2
Catch2 is a C++ test framework that uses TEST_CASE and REQUIRE/CHECK macros. Assertions are ordinary C++ expressions: REQUIRE(add(2, 2) == 4). Sections nest inside a test case to share setup without a fixture class.
Detailed Explanation
Catch2 aims for tests that read like the property they check. TEST_CASE("add", "[math]") names the case and optional tags. SECTION("negatives") runs the enclosing setup again for that branch: Catch2 re-runs the TEST_CASE from the top for each section leaf. That is how you share a parsed config without a fixture class.
REQUIRE aborts the case on failure. CHECK records and continues. REQUIRE_FALSE, CHECK_THAT with matchers (WithinAbs, StartsWith), REQUIRE_THROWS_AS, REQUIRE_NOTHROW cover the rest. The failed expression is decomposed: REQUIRE(a == b) prints both sides without a special EQ macro.
You compile one translation unit with #define CATCH_CONFIG_MAIN (or CATCH_CONFIG_RUNNER) and the Catch2 header, or you link the compiled Catch2 library in v3. v2 was header-only. v3 is a proper library and is what you should start with.
Tags ([math][slow]) select subsets on the command line. -s shows passing output. --rng-seed and shuffling exist. Generators (GENERATE(1, 2, 3)) parametrize a case.
Catch2 does not include a mock library. Use FakeIt, trompeloeil, or hand-written fakes. Death tests are not a first-class gtest-style feature; you test crash paths with a subprocess or you do not.
Choose Catch2 when you want expression-based assertions and section-style setup. Choose GoogleTest when the team already has gMock, death tests, and CMake recipes. Do not mix both in one binary without a careful main.
BDD macros SCENARIO / GIVEN / WHEN / THEN are sections with names. They do not change the runner.
Code Examples
#include <catch2/catch_test_macros.hpp>
int add(int a, int b) { return a + b; }
TEST_CASE("add", "[math]") {
REQUIRE(add(2, 2) == 4);
CHECK(add(-1, 1) == 0);
}#include <catch2/catch_test_macros.hpp>
#include <vector>
TEST_CASE("vector grow") {
std::vector<int> v;
SECTION("empty") {
REQUIRE(v.empty());
}
SECTION("one push") {
v.push_back(1);
REQUIRE(v.size() == 1);
}
}Common Gotchas
Sections re-run the whole case
a `SECTION` is not a goto. Side effects before the section run again. Keep setup cheap or use a fixture-like RAII object.
v2 versus v3 includes
v3 uses `catch2/catch_test_macros.hpp`. Old `#include "catch.hpp"` is v2.
`REQUIRE` inside a helper
like gtest `ASSERT`, it aborts the helper via an exception Catch2 throws. That is supported, unlike gtest's `ASSERT` in helpers.
Two mains
one TU with `CATCH_CONFIG_MAIN`, or the Catch2-provided main, not both.