# Catch2 — Modern C++ Test Framework with Natural Expressions > Catch2 is a header-only C++ testing framework known for its expressive assertion syntax and zero-config setup. ## Install Save in your project root: # Catch2 — Modern C++ Test Framework with Natural Expressions ## Quick Use ```bash # Single-header install (v2) or CMake FetchContent (v3) include(FetchContent) FetchContent_Declare(Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v3.7.1) FetchContent_MakeAvailable(Catch2) # Link target_link_libraries(my_tests PRIVATE Catch2::Catch2WithMain) # Run ./my_tests --success ``` ## 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` / `THEN` sections - Generates test cases from template parameters and data generators - Provides built-in micro-benchmarking with `BENCHMARK` blocks - 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 junit` for CI-compatible XML output - Tag tests with `[unit]`, `[integration]` etc. and filter with `--tags` - Set `CATCH_CONFIG_FAST_COMPILE` to 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. ## Sources - https://github.com/catchorg/Catch2 - https://catch2.readthedocs.io/ --- Source: https://tokrepo.com/en/workflows/c57a008a-4277-11f1-9bc6-00163e2b0d79 Author: AI Open Source