Catch2 Matchers C++: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Catch2 Matchers for C++: Complete Guide with Examples

1. What Are Catch2 Matchers and Why Use Them?

The Catch2 (C++ Automated Test Cases in Headers) library is a modern, header-only unit testing framework for C++. One of its most powerful features is the Matchers system — declarative value-checking tools that make your tests more readable, flexible, and informative.

Unlike traditional assertions like REQUIRE(x == 5), matchers let you express complex conditions in a natural, almost human-readable way. For example, instead of writing a loop to check every element in a vector, you can use REQUIRE_THAT(vec, Catch::Matchers::AllMatch(Catch::Matchers::Predicate<int>([](int i) { return i > 0; }))). This not only reduces code but also makes your testing intentions crystal clear.

Why use matchers? First, they offer high expressiveness: you can check if a string contains a substring, starts with a prefix, or if a number falls within a range — all with a single, concise construct. Second, matchers provide detailed diagnostics on test failure — instead of a dry "false," you get a meaningful message like "Expected: starts with 'Hello' Actual: 'World'". Third, they are composable: you can combine simple matchers into complex expressions using logical operators (And, Or, Not).

2. Installation

Catch2 is a header-only library, so no complex installation or build setup is required. There are two main ways to get started:

Method 1: Download a Single File (Recommended for Beginners)

Download catch_amalgamated.hpp or catch.hpp from the official Catch2 GitHub releases page. Place it in your project folder and include it in your code:

#include "catch_amalgamated.hpp"

Method 2: Use a Dependency Manager (vcpkg, Conan, CMake FetchContent)

For professional projects, a package manager is recommended. Example with vcpkg:

# Install Catch2 via vcpkg
vcpkg install catch2

Then in your CMakeLists.txt:

find_package(Catch2 REQUIRED)
target_link_libraries(my_test PRIVATE Catch2::Catch2WithMain)

Important: In Catch2 v3.x and later, matchers live in the Catch::Matchers namespace. In v2.x, they were also in Catch::Matchers (same namespace, but the header file structure changed). For v3.x, use #include <catch2/catch_test_macros.hpp> and #include <catch2/matchers/catch_matchers_all.hpp>.

3. Quick Start — Minimal Working Example

Let's write a simple test using a matcher to check a string:

// test.cpp
#define CATCH_CONFIG_MAIN  // This line must be in only one .cpp file
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_all.hpp>
#include <string>

TEST_CASE("String matchers work", "[string]") {
    std::string hello = "Hello, World!";

    // Check that the string contains a substring
    CHECK_THAT(hello, Catch::Matchers::ContainsSubstring("World"));

    // Check that the string starts with "Hello"
    CHECK_THAT(hello, Catch::Matchers::StartsWith("Hello"));

    // Check that the string ends with "!"
    CHECK_THAT(hello, Catch::Matchers::EndsWith("!"));
}

Recommendations