如何在几个Catch2测试用例中检查相同条件

时间:2018-09-06 08:30:50

标签: 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();
}

1 个答案:

答案 0 :(得分:2)

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
    }
}