c++ - 如何在多个 Catch2 测试用例中检查相同的条件

标签 c++ testing catch2

我必须检查几个测试用例中的某些条件(例如初始状态)。我无法在函数中使用 CHECK,如果可能的话,我想替换当前的宏。

#include "catch.hpp"

#define CHECK_INITIAL_STATE() \
    CHECK(first_condition); \
    CHECK(second_condition);

TEST_CASE("first_test") {
    CHECK_INITIAL_STATE();
    // do something
    // restore state
    CHECK_INITIAL_STATE();
}

最佳答案

Catch2 以非常优雅的方式内置了此功能:

TEST_CASE("first_test") {
    CHECK(first_condition);
    CHECK(second_condition);

    SECTION("do something 1") {
        // this test is executed after the code outside of the section
    }
    SECTION("do something 2") {
        // this test is executed after the code outside of the section
        // but without executing the previous section
    }
}

关于c++ - 如何在多个 Catch2 测试用例中检查相同的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52199773/

相关文章:

c++ - 返回类型很长的表达式

reactjs - 如何*同时*将 ngrok 用于后端和前端?

ruby-on-rails - Rails ActiveSupport::TestCase - 彩色输出

c++ - 如何使用 Catch2 和 CMake 添加单独的测试文件?

c++ - Catch2 要求异常等于某个值

c++ - 如何将 Catch2 作为外部库与 CMake 集成?

c++ - GCC -I 破坏标准库编译

c++ - 如何在 C++ 中使用 fstream get() 获取 int

c++ - 如何使用桶排序对一组字符串进行排序

testing - 您如何记录您的网络浏览器要求?