Dev

Official Release of Catch2 v3: Ushering in a New Era for C++ Testing

Catch2, the C++ testing framework, has officially released version 3. Transitioning to a standard library structure, it enhances BDD macros and microbenchmarking features.

5 min read Reviewed & edited by the SINGULISM Editorial Team

Official Release of Catch2 v3: Ushering in a New Era for C++ Testing
Photo by Graphic Cabin on Unsplash

Catch2, a widely-used unit testing framework for C++, has officially launched version 3, marking a major upgrade. This release, which has garnered attention on GitHub Trending, includes a significant structural shift from a single-header format to a conventional library architecture.

What is Catch2?

Catch2 is a unit testing framework for C++ that also provides microbenchmarking capabilities and BDD (Behavior-Driven Development) macros. Its standout feature is its “simple and natural” test-writing process. Test names do not need to be valid identifiers, and assertions can be written as regular C++ boolean expressions. Additionally, its section mechanism allows for localized sharing of setup and teardown code between tests.

The usability of Catch2 is evident in the code examples shared in the catchorg repository on GitHub.

#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); 
} 

Testing the factorial function is as intuitive as shown above.

Major Changes in v3

The most significant change in Catch2 v3 is the transition from a single-header library to a standard library structure. Previously, Catch2 could be used by simply including a single header file. However, with version 3, it now comprises multiple headers and separately compiled implementations.

This change brings several advantages, such as reduced compilation times for large projects, clearer dependency management, and the ability to select features on a modular basis. However, this shift comes at the expense of ease of integration, as the single-header approach was more straightforward for smaller projects. This change means that project teams will need to choose the best option based on their project’s size and requirements.

Microbenchmarking Feature

One of Catch2’s standout features is its integrated microbenchmarking capability. Beyond unit testing, it enables performance measurements within the same framework, distinguishing it from competing frameworks like Google Test.

The code examples shared on GitHub Trending also demonstrate the use of the benchmark feature.

#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); 
}; 
} 

By default, benchmarks are not executed. They are activated explicitly using the [!benchmark] tag. This allows for flexible use, where benchmarks can be skipped during regular test runs and executed only when performance measurement is needed.

Migration Guide and Ecosystem

For those transitioning from Catch2 v2 to v3, the official documentation provides a migration guide. This guide covers everything from basic setup methods to common migration challenges. The older version, Catch2 v2.x, remains available on GitHub under the v2.x branch.

Bug reports for the project are accepted via the GitHub Issue Tracker, while discussions and inquiries are hosted on a dedicated Discord channel. Catch2 has been widely adopted within the open-source community and has also been used in commercial projects.

The evolution of Catch2 provides valuable insights into decision-making in software projects. For instance, much like Samsung’s decision to discontinue certain health features on the Galaxy Watch in the U.S. or Anthropic’s declaration of reviving Fable 5, Catch2’s transition to v3 represents a strategic shift. Similarly, as seen with Commodore’s $100 price cut for its retro phone Callback 8020, market positioning is an ongoing process. The move to Catch2 v3 can also be viewed as a strategic decision in line with these examples.

Editorial Opinion

The release of Catch2 v3 has the potential to bring seismic changes to the C++ testing framework landscape. The move from a single-header format to a conventional library structure sacrifices ease of integration but improves manageability and compilation performance for large-scale projects. The adoption trends in major C++ projects over the next 3–6 months will serve as a key indicator of the market’s direction.

From a long-term perspective, distinctive features like BDD macros and microbenchmarking capabilities may help Catch2 stand out from competitors like Google Test and Boost.Test. Its positioning as a comprehensive quality assurance tool beyond just a unit testing framework could significantly influence its broader adoption.

The decision to transition to Catch2 v3 will likely impact the overall quality management strategies of C++ projects. It remains to be seen how this shift will affect small-scale projects and educational use cases that relied on the simplicity of the single-header format. Observing the community’s response will be crucial in evaluating the long-term implications of this decision.

References

Frequently Asked Questions

What are the differences between Catch2 and Google Test?
Catch2 allows the use of arbitrary strings for test names and supports assertions written in natural C++ expressions, making tests more intuitive to write than in Google Test. It also includes built-in BDD macros and microbenchmarking features. While Google Test offers a broader set of features and better integration with Google's ecosystem, Catch2 prioritizes simplicity and ease of use.
What should I be aware of when migrating from v2 to v3?
The biggest change is the switch from a single-header structure to a standard library format. Unlike before, you can no longer use it by simply including one header file; you must now link against precompiled implementations. The official migration guide addresses common issues and their solutions.
What license does Catch2 use?
Catch2 is distributed under the Boost Software License 1.0. This license permits broad usage, including commercial applications, as well as redistribution and modification, provided the copyright notice and license terms are retained.
Source: GitHub Trending

Comments

← Back to Home