gpt4 book ai didi

c++ - 如果所有部分都失败,Catch2 将再次运行测试

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:40:18 26 4
gpt4 key购买 nike

我有以下使用最新 (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 运行每个部分一次并告诉我第一个失败.但是,如果我在 both 部分中将断言更改为 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 文档,但找不到任何相关内容。

最佳答案

来自docs :

One leaf section is executed on each run through a TEST_CASE. The other sections are skipped. Next time through the next section is executed, and so on until no new sections are encountered.

所以有问题的测试必须运行 3 次。

关于c++ - 如果所有部分都失败,Catch2 将再次运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52432147/

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