Favicon of Catch2

Catch2

Catch2 is a C++14/17 unit testing framework with BDD-style macros and built-in micro-benchmarking, distributed as a library.

Catch2 website screenshot
Catch2 GitHub repository preview

Catch2 is a testing framework built specifically for C++, aimed at developers who want unit tests, TDD workflows, or BDD-style specs without pulling in a heavy dependency chain. Test names don't need to be valid identifiers, assertions read like normal boolean expressions, and sections let you share setup and teardown code locally inside a test case. It's a good fit for teams writing C++14 or later who want a test runner that feels natural to write in, rather than one bolted on from another language's testing conventions.

Key features

  • Natural assertions: REQUIRE and CHECK macros wrap ordinary C++ boolean expressions, so you don't learn a separate assertion DSL.
  • Free-form test names: test case names are strings, not identifiers, so you can describe intent directly ("Factorials are computed").
  • Sections: a mechanism for sharing setup/teardown code across related checks inside one test case, avoiding fixture boilerplate.
  • BDD macros: GIVEN, WHEN, THEN style macros for teams that prefer behavior-driven test structure.
  • Micro-benchmarking: a built-in BENCHMARK macro for simple performance measurement inside test cases, opt-in via a [!benchmark] tag.
  • No external dependencies: Catch2 doesn't require any third-party libraries to build or run.
  • Library, not single header: version 3 ships as a compiled library with multiple headers instead of the old single-header drop-in, which speeds up build times on larger projects.

Ideal use cases

Catch2 fits projects already written in C++14 or newer that need a straightforward unit testing setup, whether that's a small library, an application, or a larger codebase migrating off an older or custom test harness. It works well for teams that want readable test output and don't want to write custom fixture classes for every shared setup step. The BDD macros make it a reasonable choice for teams that already think in given/when/then terms, and the benchmarking macros are useful for quick, in-tree performance checks without adding a separate benchmarking tool.

It's not the right choice if you're stuck on C++11, since that support only exists in the older v2 branch. If you need a dedicated, statistically rigorous benchmarking tool, Catch2's built-in benchmarks are convenient but not a replacement for something purpose-built like Google Benchmark. Projects that specifically want a single-header drop-in (no build step) should stay on v2, since v3 is a compiled library.

Installation

Catch2 v3 is distributed as a library rather than a single header, so it needs to be built and linked rather than just included. The common approach is to pull it in with CMake's FetchContent:

Include(FetchContent)

FetchContent_Declare(
  Catch2
  GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  GIT_TAG        v3.x)

FetchContent_MakeAvailable(Catch2)

target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)

Alternatively, if Catch2 is already installed on the system (via a package manager or manual build/install), use find_package:

find_package(Catch2 3 REQUIRED)
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)

A basic test file looks like this:

#include <catch2/catch_test_macros.hpp>

TEST_CASE("Factorials are computed", "[factorial]") {
    REQUIRE(factorial(1) == 1);
}

If you're migrating an existing project from Catch2 v2, check the migration guide in the docs folder first, since the switch from single-header to library changes include paths and build setup. For teams that need C++11 support, use the v2.x branch instead of devel.

Categories:

Frequently asked questions

Share:

Stars
21.4K
Forks
3.4K
Last commit
13 hours ago
Repository age
16 years
License
BSL-1.0
Self-hosted
No
Activity score
83/100
View Repository
Built with:
Ad
Favicon

 

  
 

Similar to Catch2

Favicon

 

  
 
Favicon

 

  
 
Favicon