catchorg/Catch2

GitHub: catchorg/Catch2

一个现代化的 C++ 原生测试框架,支持单元测试、TDD 和 BDD,并提供基础的微基准测试功能。

Stars: 20209 | Forks: 3201

![Catch2 logo](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/fed71aef40143638.svg) [![Github Releases](https://img.shields.io/github/release/catchorg/catch2.svg)](https://github.com/catchorg/catch2/releases) [![Linux build status](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/798017cb11143639.svg)](https://github.com/catchorg/Catch2/actions/workflows/linux-simple-builds.yml) [![Linux build status](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/ca591def3a143640.svg)](https://github.com/catchorg/Catch2/actions/workflows/linux-other-builds.yml) [![MacOS build status](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/28a14ddea4143640.svg)](https://github.com/catchorg/Catch2/actions/workflows/mac-builds.yml) [![Build Status](https://ci.appveyor.com/api/projects/status/github/catchorg/Catch2?svg=true&branch=devel)](https://ci.appveyor.com/project/catchorg/catch2) [![Code Coverage](https://codecov.io/gh/catchorg/Catch2/branch/devel/graph/badge.svg)](https://codecov.io/gh/catchorg/Catch2) [![Try online](https://img.shields.io/badge/try-online-blue.svg)](https://godbolt.org/z/EdoY15q9G) [![Join the chat in Discord: https://discord.gg/4CWS9zD](https://img.shields.io/badge/Discord-Chat!-brightgreen.svg)](https://discord.gg/4CWS9zD) ## 什么是 Catch2? Catch2 主要是一个 C++ 单元测试框架,但它也 提供基本的微基准测试功能,以及简单的 BDD 宏。 Catch2 的主要优势在于使用它既简单又自然。 测试名称不必是有效的标识符,断言看起来像 标准的 C++ 布尔表达式,并且 sections 提供了一种在测试中 共享 set-up 和 tear-down 代码的好方法。 **单元测试示例** ``` #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 ); } ``` **微基准测试示例** ``` #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); }; } ``` _注意,默认情况下不运行基准测试,因此你需要使用 `[!benchmark]` 标签显式运行它。_ ## Catch2 v3 已发布! 你正在查看的是 `devel` 分支,这是 v3 版本正在开发的分支。 v3 带来了一系列重大变化,其中最大的一点是 Catch2 不再是一个单头文件库。Catch2 现在的行为像一个普通的 库,具有多个头文件和单独编译的实现。 文档正在慢慢更新以适应这些变化, 但这方面的工作目前仍在进行中。 要从 v2 版本迁移到 v3,你应该查看 [我们的 文档](docs/migrate-v2-to-v3.md#top)。它提供了简单的 入门指南,并收集了最常见的迁移 问题。 对于 Catch2 的上一个主要版本,请[在 GitHub 上查看 `v2.x` 分支 ](https://github.com/catchorg/Catch2/tree/v2.x)。 ## 如何使用 本文档包含以下三个部分: * [为什么我们需要另一个 C++ 测试框架?](docs/why-catch.md#top) * [教程](docs/tutorial.md#top) - 入门指南 * [参考部分](docs/Readme.md#top) - 所有详细信息 ## 更多 * 问题 和错误可以在 [GitHub Issue tracker](https://github.com/catchorg/Catch2/issues) 上提出 * 对于讨论或提问,请使用 [我们的 Discord](https://discord.gg/4CWS9zD) * 看看还有谁在使用 Catch2,请查看 [开源软件](docs/opensource-users.md#top) 或 [商业用途](docs/commercial-users.md#top)。
标签:Bash脚本, BDD, C++, C++14, C++17, Catch2, DNS解析, Header-only, HTTP头分析, TDD, 单元测试, 开源框架, 开源项目, 微基准测试, 持续集成, 数据擦除, 测试框架, 测试驱动开发, 行为驱动开发