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

Catch2 runs test once more if all sections fail

我有以下代码,我 运行 使用最新 (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 运行s 每个部分一次并告诉我第一个失败。但是,如果我在 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

考试明明是运行三次,最后一次绕过了两个section。这种行为是预期的吗?我试过查看 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次。