Who: Created by Phil Nash, now maintained by the Catch2 community on GitHub.
Why: To provide a simpler, more expressive alternative to Google Test — with natural language test names, no separate fixture classes, and a single-header option.
When: Catch (v1) released around 2010. Catch2 (v2) in 2017. Catch2 v3 (compiled, no longer single-header) in 2022.
Introduction
What is Catch2?
A modern C++ testing framework focused on simplicity and expressiveness.
Tests read like natural language. No separate fixture class needed.
#include <catch2/generators/catch_generators_all.hpp>TEST_CASE("Square is always non-negative", "[generators]") { auto x = GENERATE(range(-5, 6)); // -5, -4, ..., 5 REQUIRE(x * x >= 0);}TEST_CASE("String operations", "[generators]") { auto s = GENERATE( std::string("hello"), std::string("world"), std::string("catch2") ); REQUIRE_FALSE(s.empty());}// Cartesian productTEST_CASE("All pairs", "[generators]") { auto a = GENERATE(1, 2, 3); auto b = GENERATE(10, 20); REQUIRE(a * b > 0); // runs 6 times (3 × 2)}
Exception Testing
TEST_CASE("Exception handling") { REQUIRE_THROWS(throw std::runtime_error("oops")); REQUIRE_THROWS_AS(throw std::invalid_argument("bad"), std::invalid_argument); REQUIRE_NOTHROW([]{ int x = 1 + 1; }()); // With matchers REQUIRE_THROWS_WITH( throw std::runtime_error("file not found"), ContainsSubstring("not found") );}
Running Tests
# Run all tests./my_tests# Run by tag./my_tests "[math]"# Run by name (partial match)./my_tests "Factorial"# List all tests./my_tests --list-tests# Verbose output./my_tests -v# Run with CTestcd build && ctest --output-on-failure