2

我必须检查几个测试用例中的某些条件(例如初始状态)。我不能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();
}
4

1 回答 1

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
    }
}
于 2018-09-06T08:34:13.703 回答