Introduction
Catch2 is a C++ test framework that favors natural, readable test expressions over macro-heavy assertion APIs. It requires no external dependencies and integrates into any build system. Version 3 moved from a single header to a proper library while keeping the same expressive syntax.
What Catch2 Does
- Lets you write assertions as plain C++ expressions:
REQUIRE(x == 42) - Supports BDD-style
SCENARIO/GIVEN/WHEN/THENsections - Generates test cases from template parameters and data generators
- Provides built-in micro-benchmarking with
BENCHMARKblocks - Outputs results in JUnit XML, TAP, or compact console formats
Architecture Overview
Catch2 v3 ships as a static library built with CMake. Test cases register themselves via static constructors into a global registry. The runner walks the registry, executing each section as an independent path through the test function. Generators produce lazy value sequences that expand into separate sub-cases at runtime, keeping memory use constant regardless of data set size.
Self-Hosting & Configuration
- Install via CMake FetchContent, vcpkg, Conan, or your system package manager
- Use
--reporter junitfor CI-compatible XML output - Tag tests with
[unit],[integration]etc. and filter with--tags - Set
CATCH_CONFIG_FAST_COMPILEto reduce compile times on large suites - Integrate with CTest using the
catch_discover_tests()CMake helper
Key Features
- Natural expression assertions decompose both sides on failure automatically
- Section-based tests replace fixtures with scoped setup/teardown blocks
- Data generators (
GENERATE) create parameterized tests inline - Tag-based filtering lets you run subsets without recompiling
- Built-in benchmarking produces min/mean/max timing statistics
Comparison with Similar Tools
- GoogleTest — more enterprise features and Google Mock integration; Catch2 has simpler setup and friendlier syntax
- doctest — even faster compile times; Catch2 offers richer generators and BDD sections
- Boost.Test — deep Boost ecosystem integration; Catch2 is standalone with fewer dependencies
- CppUnit — traditional xUnit style; Catch2 provides more expressive assertions
- Criterion — C-focused with parallel runners; Catch2 is C++-native with richer feature set
FAQ
Q: Should I use Catch2 v2 or v3? A: Version 3 is the actively maintained branch. It replaces the single header with a library for faster compiles.
Q: Can Catch2 run tests in parallel? A: Catch2 does not parallelize internally. Use CTest or a CI matrix to run test binaries concurrently.
Q: How do I integrate Catch2 with Bazel?
A: Use rules_foreign_cc to build Catch2 with CMake inside Bazel, or vendor the built library as a precompiled dependency.
Q: Does Catch2 support mocking? A: Catch2 has no built-in mocking. Pair it with trompeloeil, FakeIt, or any C++ mocking library.