Catch2 README

The project's README from github.com/catchorg/Catch2. As one markdown file: README.md.

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

Catch2 (catchorg/Catch2)

Searching Database…

21.5K ·GitHub
这是什么?Catch2 是一个 C++ 单元测试框架,同时还提供基础的微基准测试功能和简单的 BDD 宏。
所以它是一个要编译进我自己测试可执行文件里的库,而不是一个独立的工具?是的——你用 C++ 编写测试,然后像使用其他库一样针对 Catch2 进行编译。它以头文件加上一份单独编译的实现的形式发布,你需要把这份实现链接到你的测试可执行文件里。
我怎么编写并运行我的第一个测试?从文档中的入门教程开始;文档里还有一个完整的参考章节涵盖各种细节。文档分为三部分:这个框架为什么存在、教程,以及参考手册。
它需要哪个 C++ 标准,会不会引入其他依赖库?v3 面向 C++14 及更高版本(C++17 也没问题),它是纯 C++ 实现的,没有任何外部依赖。还卡在 C++11?那就用 v2.x 分支;C++03 则要去看 Catch1.x 分支。
v3 和 v2 相比有什么不同?最大的变化是 v3 不再是单头文件库——以前只要放进一个头文件就能用的构建方式,现在必须编译并链接一个普通的库。文档里有一份迁移指南,汇总了最常见的问题。
真的有实际项目在用 Catch2 吗?有——README 里列出了使用它的开源项目和商业公司,而且这个仓库从 2010 年起就一直在 GitHub 上。
和其他 C++ 测试框架相比,Catch2 为什么让写测试更轻松?测试名称不必是合法的标识符,断言写起来就像普通的 C++ 布尔表达式,而且 section 提供了一种优雅又局部化的方式,可以在测试中共享 setup 和 teardown 代码。
我怎么运行微基准测试?Catch2 内置了基础的微基准测试功能,但基准测试默认不会运行——你必须使用 [!benchmark] 标签显式地运行它们。
为什么又造一个 C++ 测试框架?文档开篇就是这个问题,并在教程之前给出了理由。简单来说,作者想要一个让编写测试感觉简单自然的框架。
What are similar projects?mocha, ava, testify, googletest, jasmine, cppinsights, coost, pest
Creator
catchorg
Size
33 MB
Stars
21.5K
Age
15y
Commits · 90 Days