gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:05:20 25 4
gpt4 key购买 nike

我必须在几个测试用例中检查某些条件(例如初始状态)。我无法在函数中使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com