catchorg / Catch2
catchorg / Catch2
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.
什么是 Catch2?Catch2 主要是一个 C++ 单元测试框架,但它也提供了基础的微基准测试功能和简单的 BDD(行为驱动开发)宏。Catch2 的主要优势在于其使用方式既简单又自然。测试名称不必是有效的标识符,断言看起来就像普通的 C++ 布尔表达式,而“节”(sections)则提供了一种优雅且局部化的方式来共享测试中的设置(set-up)和清理(tear-down)代码。
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.
请注意,基准测试默认不会运行,因此你需要使用 [!benchmark] 标签显式地运行它们。
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.
Catch2 v3 已经发布!你当前位于 devel 分支,这是 v3 版本的开发分支。v3 带来了许多重大变化,其中最主要的是 Catch2 不再是一个单头文件库。Catch2 现在表现为一个标准的库,包含多个头文件和单独编译的实现。
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.
文档正在逐步更新以反映这些变化,但目前工作仍在进行中。若要从 v2 版本迁移到 v3,请查阅我们的文档。它提供了简单的入门指南,并汇总了最常见的迁移问题。关于 Catch2 的前一个主要版本,请查看 GitHub 上的 v2.x 分支。
How to use it
如何使用
This documentation comprises these three parts: 本说明文档包含以下三个部分:
- Why do we need yet another C++ Test Framework? 为什么我们需要另一个 C++ 测试框架?
- Tutorial - getting started 教程 - 入门指南
- Reference section - all the details 参考章节 - 详细信息
More Issues and bugs can be raised on the Issue tracker on GitHub. For discussion or questions please use our Discord. See who else is using Catch2 in Open Source Software or commercially.
更多问题和 Bug 可在 GitHub 的 Issue 追踪器上提出。如需讨论或提问,请使用我们的 Discord。查看还有谁在开源软件或商业项目中使用了 Catch2。