Google Test (often called gtest) provides TEST() macros for individual test cases, ASSERT_*/EXPECT_* macros for assertions (ASSERT stops the current test on failure, EXPECT continues to report additional failures), and test fixtures for shared setup. Catch2 takes a lighter-weight, header-only approach requiring no separate library build step, using TEST_CASE and expressive REQUIRE/CHECK macros. Both integrate with CMake for automatic test discovery and CI execution; the choice between them is largely a matter of project convention rather than one being definitively superior.
A basic test with Google Test
ASSERT_EQ stops this specific test immediately on failure; EXPECT_EQ would instead record the failure and let the rest of the test continue running, useful for seeing multiple failures from one test run.
#include <gtest/gtest.h>
#include "calculator.h"
TEST(CalculatorTest, AddsTwoNumbers) {
Calculator calc;
ASSERT_EQ(calc.add(2, 3), 5);
}
TEST(CalculatorTest, ThrowsOnDivideByZero) {
Calculator calc;
ASSERT_THROW(calc.divide(1, 0), std::invalid_argument);
}The same test with Catch2
Catch2's syntax reads closer to natural language, and being header-only means no separate library needs to be built and linked — just include the header.
#include <catch2/catch_test_macros.hpp>
#include "calculator.h"
TEST_CASE("Calculator adds two numbers", "[calculator]") {
Calculator calc;
REQUIRE(calc.add(2, 3) == 5);
}Test fixtures: shared setup across tests
Deriving a test from a fixture class gives every TEST_F in that fixture a fresh instance, avoiding repeated setup code in each individual test and preventing tests from accidentally sharing state.
class CalculatorTest : public ::testing::Test {
protected:
Calculator calc; // fresh instance for EVERY test using this fixture
};
TEST_F(CalculatorTest, AddsTwoNumbers) {
ASSERT_EQ(calc.add(2, 3), 5);
}