Convert Figma logo to code with AI

catchorg logoCatch2

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)

18,461
3,021
18,461
421

Top Related Projects

18,461

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)

GoogleTest - Google Testing and Mocking Framework

5,809

The fastest feature-rich C++11/14/17/20/23 single-header testing framework

1,246

C++20 μ(micro)/Unit Testing Framework

20,408

A modern formatting library

Quick Overview

Catch2 is a modern, C++-native, header-only test framework for unit-tests, TDD and BDD. It's designed to be easy to use, flexible, and highly portable, supporting a wide range of C++ compilers and versions.

Pros

  • Header-only library, easy to integrate into projects
  • Supports both traditional unit testing and Behavior-Driven Development (BDD)
  • Extensive set of assertion macros and powerful matchers
  • Excellent support for C++11 and later versions

Cons

  • Can increase compile times due to being header-only
  • Learning curve for advanced features and custom matchers
  • Limited built-in support for parameterized tests compared to some other frameworks

Code Examples

  1. Basic test case:
#include <catch2/catch_test_macros.hpp>

TEST_CASE("Addition works", "[math]") {
    REQUIRE(1 + 1 == 2);
    CHECK(2 + 2 == 4);
}
  1. BDD-style test:
#include <catch2/catch_test_macros.hpp>

SCENARIO("Vector can be sized and resized", "[vector]") {
    GIVEN("A vector with some items") {
        std::vector<int> v(5);

        REQUIRE(v.size() == 5);
        REQUIRE(v.capacity() >= 5);

        WHEN("the size is increased") {
            v.resize(10);

            THEN("the size and capacity change") {
                REQUIRE(v.size() == 10);
                REQUIRE(v.capacity() >= 10);
            }
        }
    }
}
  1. Using matchers:
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>

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

    REQUIRE_THAT(text, Catch::Matchers::StartsWith("Hello"));
    REQUIRE_THAT(text, Catch::Matchers::EndsWith("World!"));
    REQUIRE_THAT(text, Catch::Matchers::ContainsSubstring("lo, Wo"));
}

Getting Started

  1. Download the single header file from the Catch2 GitHub repository.
  2. Include the header in your test file:
#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() function
#include "catch.hpp"

TEST_CASE("My first test", "[tag]") {
    REQUIRE(true);
}
  1. Compile and run your test file:
g++ -std=c++11 -Wall -Wextra -Wpedantic -o test_runner test_file.cpp
./test_runner

Competitor Comparisons

18,461

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)

Pros of Catch2

  • Established and widely adopted testing framework for C++
  • Extensive documentation and community support
  • Supports a wide range of C++ versions and compilers

Cons of Catch2

  • Larger codebase and potentially slower compilation times
  • May have more features than needed for simple projects
  • Requires more setup and configuration for advanced use cases

Code Comparison

Both repositories contain the same codebase, as they are the same project. Here's a sample of how a basic test might look in Catch2:

#include <catch2/catch_test_macros.hpp>

TEST_CASE("Addition works", "[math]") {
    REQUIRE(1 + 1 == 2);
    CHECK(2 + 2 == 4);
}

Summary

The comparison between Catch2 and Catch2 is not applicable, as they are the same project. The repository catchorg/Catch2 is the official repository for the Catch2 testing framework. It's a popular, header-only C++ testing framework known for its simplicity and powerful features. The framework is widely used in the C++ community and provides a robust set of tools for unit testing and BDD-style specifications.

GoogleTest - Google Testing and Mocking Framework

Pros of GoogleTest

  • More extensive feature set, including mocking capabilities with GoogleMock
  • Better support for parameterized tests and test fixtures
  • Stronger integration with other Google testing tools and frameworks

Cons of GoogleTest

  • Steeper learning curve due to more complex API and setup
  • Requires separate compilation, increasing build times
  • Less intuitive syntax for writing tests compared to Catch2

Code Comparison

GoogleTest:

TEST(FactorialTest, HandlesZeroInput) {
  EXPECT_EQ(Factorial(0), 1);
}

TEST_F(QueueTest, IsEmptyInitially) {
  EXPECT_TRUE(queue.IsEmpty());
}

Catch2:

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

TEST_CASE("Queue is empty initially", "[queue]") {
  Queue queue;
  REQUIRE(queue.IsEmpty());
}

Both GoogleTest and Catch2 are popular C++ testing frameworks. GoogleTest offers a more comprehensive set of features and better integration with other Google tools, but comes with a steeper learning curve. Catch2, on the other hand, provides a simpler, header-only solution with more intuitive syntax, making it easier for beginners to get started with testing.

5,809

The fastest feature-rich C++11/14/17/20/23 single-header testing framework

Pros of doctest

  • Faster compile times due to header-only implementation
  • Simpler setup with a single header file
  • Smaller binary size and lower memory footprint

Cons of doctest

  • Less extensive feature set compared to Catch2
  • Fewer assertion macros and test case variants
  • Limited support for BDD-style syntax

Code Comparison

doctest:

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

TEST_CASE("addition") {
    CHECK(1 + 1 == 2);
}

Catch2:

#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

TEST_CASE("addition", "[math]") {
    REQUIRE(1 + 1 == 2);
}

Both frameworks offer a simple syntax for writing tests, but Catch2 provides more options for test case organization and labeling. doctest's implementation is more compact, which contributes to its faster compile times and smaller footprint.

While doctest excels in simplicity and performance, Catch2 offers a more comprehensive set of features and greater flexibility in test organization. The choice between the two depends on project requirements, with doctest being ideal for projects prioritizing speed and simplicity, and Catch2 better suited for larger projects requiring more advanced testing capabilities.

1,246

C++20 μ(micro)/Unit Testing Framework

Pros of ut

  • Faster compilation times and smaller binary sizes
  • Header-only library with no external dependencies
  • Supports C++20 features and concepts

Cons of ut

  • Less mature and less widely adopted than Catch2
  • Fewer features and less extensive documentation
  • May require more recent compiler versions for full functionality

Code Comparison

Catch2:

#include <catch2/catch_test_macros.hpp>

TEST_CASE("Addition works") {
    REQUIRE(1 + 1 == 2);
    CHECK(2 + 2 == 4);
}

ut:

#include <boost/ut.hpp>

int main() {
  using namespace boost::ut;
  "Addition works"_test = [] {
    expect(1 + 1 == 2_i);
    expect(2 + 2 == 4_i);
  };
}

Both libraries offer concise syntax for writing unit tests, but ut uses a more modern C++ approach with its inline literals and lambda-based test definitions. Catch2 follows a more traditional macro-based style, which may be more familiar to some developers.

20,408

A modern formatting library

Pros of fmt

  • Focused on string formatting and output, providing a more specialized and optimized solution
  • Offers a simpler API for common formatting tasks, making it easier to use for basic string operations
  • Provides better performance for string formatting compared to alternatives like std::stringstream

Cons of fmt

  • Limited to string formatting and output, lacking the comprehensive testing features of Catch2
  • May require additional libraries or tools for more complex testing scenarios
  • Less suitable for writing unit tests and performing assertions

Code Comparison

fmt:

#include <fmt/core.h>

std::string result = fmt::format("Hello, {}!", "world");
fmt::print("The result is: {}\n", result);

Catch2:

#include <catch2/catch_test_macros.hpp>

TEST_CASE("My test case") {
    REQUIRE(2 + 2 == 4);
    CHECK(std::string("hello") == "hello");
}

Summary

fmt is a specialized library for string formatting and output, offering simplicity and performance for these tasks. Catch2, on the other hand, is a comprehensive testing framework designed for writing and running unit tests. While fmt excels in string manipulation, Catch2 provides a robust set of tools for test-driven development and assertion checking.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Catch2 logo

Github Releases Linux build status Linux build status MacOS build status Build Status Code Coverage Try online Join the chat in Discord: https://discord.gg/4CWS9zD

What is Catch2?

Catch2 is mainly a unit testing framework for C++, but it also provides basic micro-benchmarking features, and simple BDD macros.

Catch2's main advantage is that using it is both simple and natural. Test names do not have to be valid identifiers, assertions look like normal C++ boolean expressions, and sections provide a nice and local way to share set-up and tear-down code in tests.

Example unit test

#include <catch2/catch_test_macros.hpp>

#include <cstdint>

uint32_t factorial( uint32_t number ) {
    return number <= 1 ? number : factorial(number-1) * number;
}

TEST_CASE( "Factorials are computed", "[factorial]" ) {
    REQUIRE( factorial( 1) == 1 );
    REQUIRE( factorial( 2) == 2 );
    REQUIRE( factorial( 3) == 6 );
    REQUIRE( factorial(10) == 3'628'800 );
}

Example microbenchmark

#include <catch2/catch_test_macros.hpp>
#include <catch2/benchmark/catch_benchmark.hpp>

#include <cstdint>

uint64_t fibonacci(uint64_t number) {
    return number < 2 ? number : fibonacci(number - 1) + fibonacci(number - 2);
}

TEST_CASE("Benchmark Fibonacci", "[!benchmark]") {
    REQUIRE(fibonacci(5) == 5);

    REQUIRE(fibonacci(20) == 6'765);
    BENCHMARK("fibonacci 20") {
        return fibonacci(20);
    };

    REQUIRE(fibonacci(25) == 75'025);
    BENCHMARK("fibonacci 25") {
        return fibonacci(25);
    };
}

Note that benchmarks are not run by default, so you need to run it explicitly with the [!benchmark] tag.

Catch2 v3 has been released!

You are on the devel branch, where the v3 version is being developed. v3 brings a bunch of significant changes, the big one being that Catch2 is no longer a single-header library. Catch2 now behaves as a normal library, with multiple headers and separately compiled implementation.

The documentation is slowly being updated to take these changes into account, but this work is currently still ongoing.

For migrating from the v2 releases to v3, you should look at our documentation. It provides a simple guidelines on getting started, and collects most common migration problems.

For the previous major version of Catch2 look into the v2.x branch here on GitHub.

How to use it

This documentation comprises these three parts:

More