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); Martin Hořeňovský (2010).
npm install catch2.cxx
|
cpp
#include
#include
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
`cpp
#include
#include
#include
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._
Installation
Run:
`bash
$ npm i catch2.cxx
`
And then include catch2.hpp as follows:
`cxx
// main.cxx
#define CATCH2_IMPLEMENTATION
#include // or, catch2/catch_all.hpp
int main() { / ... / }
`
Finally, compile while adding the path node_modules/catch2.cxx to your compiler's include paths.
`bash
$ clang++ -std=c++17 -I./node_modules/catch2.cxx main.cxx # or, use g++
$ g++ -std=c++17 -I./node_modules/catch2.cxx main.cxx
`
You may also use a simpler approach with the cpoach tool, which automatically adds the necessary include paths of all the installed dependencies for your project.
`bash
$ cpoach clang++ -std=c++17 main.cxx # or, use g++
$ cpoach g++ -std=c++17 main.cxx
`
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