下面是我使用最新版本(2.4.0) Catch2运行的代码:
#include "catch.hpp"
#include <iostream>
TEST_CASE("Test") {
int x = 0;
SECTION("A") {
std::cout << "A";
++x;
REQUIRE(x == 1);
}
SECTION("B") {
std::cout << "B";
++x;
REQUIRE(x == 1);
}
std::cout << "X\n";
REQUIRE(x == 1);
}如果我运行这个程序,一切都按预期工作,我得到:
AX
BX
=================================================================
All tests passed (4 assertions in 1 test case)显然,测试用例运行两次,每节运行一次。
如果我更改了其中一节中的断言,告诉REQUIRE(x == 0),一切都按预期工作,Catch2运行每个部分一次,并告诉我第一个部分失败了。但是,如果我在这两个部分中将断言更改为REQUIRE(x == 0),则结果是令人困惑的(我稍微缩短了它):
A
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.out is a Catch v2.4.0 host application.
Run with -? for options
---------------------------------------------------------------------
Test
A
---------------------------------------------------------------------
test.cpp:10: FAILED:
REQUIRE( x == 0 )
with expansion:
1 == 0
B--------------------------------------------------------------------
Test
B
---------------------------------------------------------------------
test.cpp:15: FAILED:
REQUIRE( x == 0 )
with expansion:
1 == 0
X
---------------------------------------------------------------------
Test
---------------------------------------------------------------------
test.cpp:19: FAILED:
REQUIRE( x == 1 )
with expansion:
0 == 1
=====================================================================
test cases: 1 | 1 failed
assertions: 3 | 3 failed测试显然运行了三次,最后一次绕过了这两个部分。这是预期的行为吗?我试过查看Catch2文档,但没有找到任何相关的内容。
发布于 2018-09-20 20:03:30
https://stackoverflow.com/questions/52432147
复制相似问题