Google Test的 EXPECT宏在期望条件为假时会抛出异常并中止测试

后端开发 2026-07-08

在Google Test单元测试框架中,EXPECT_xx 宏在预期条件为假时应该记录一次失败,但允许测试继续进行。

但在参数化测试中,一旦发现条件为假,测试就会立即以 testing::internal::GoogleTestFailureException 失败。这是预期的行为吗?我本以为应该继续运行测试,直到值列表中的所有参数都被检查完。

下面是我正在测试的类:

class IExample
{
    public:
        virtual void DoNothing() = 0;
        virtual bool ReturnBoolean() = 0;
        virtual bool IsEven(int value) = 0;
        virtual ~IExample() = default;
};

class Example : public IExample
{
    public:
        Example() = default;
        void DoNothing();
        bool ReturnBoolean();
        bool IsEven(int value) { return value % 2 == 0; }
};

下面是我的测试套件:

#include <gtest/gtest.h>

#include "Example.h"

class ParameterTest :
    public ::testing::TestWithParam<int>
{
public:

    void SetUp() override
    {
        pExample = new Example();  
    }

    void TearDown() override
    {
        delete pExample;
    }

    Example example;
    Example* pExample;

};

INSTANTIATE_TEST_SUITE_P(
    SmallNumbers, // Prefix
    ParameterTest, // Test suite
    ::testing::Values(0, 1, 2, 3, 4, 5) // Parameter values
);

TEST_P(ParameterTest, Example_IsEven)
{
    int value = GetParam();
    EXPECT_TRUE(example.IsEven(value));
}

输出如下:

[----------] 6 tests from SmallNumbers/ParameterTest
[ RUN      ] SmallNumbers/ParameterTest.Example_IsEven/0
[       OK ] SmallNumbers/ParameterTest.Example_IsEven/0 (0 ms)
[ RUN      ] SmallNumbers/ParameterTest.Example_IsEven/1
../../LNX/Logix/Engine/Subsystems/UnitTestExamples/Tests/UnitTests/ExampleTestSuite/ParameterTestSuite.cpp:34: Failure
Value of: example.IsEven(value)
  Actual: false
Expected: true

libc++abi: terminating due to uncaught exception of type testing::internal::GoogleTestFailureException: ../../LNX/Logix/Engine/Subsystems/UnitTestExamples/Tests/UnitTests/ExampleTestSuite/ParameterTestSuite.cpp:34: Failure
Value of: example.IsEven(value)
  Actual: false
Expected: true



0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.06 sec

The following tests FAILED:
        140 - Logix_Engine_Subsystems_UnitTestExamples_Tests_UnitTests_ExampleTestSuite (Subprocess aborted)
Errors while running CTest

编辑:我也用一个普通的测试夹具尝试了这一点,运行一个通过的测试、一个失败的测试,以及一个应该通过的测试。测试在失败的测试处停止。

编辑2:需要说明的是,我是在VS Code中运行这些测试。这样的行为在自动化测试环境中会改变吗?

解决方案

// This exception is thrown by (and only by) a failed Google Test
// assertion when GTEST_FLAG(throw_on_failure) is true (if exceptions
// are enabled).  We derive it from std::runtime_error, which is for
// errors presumably detectable only at run time.  Since
// std::runtime_error inherits from std::exception, many testing
// frameworks know how to extract and print the message inside it.
class GTEST_API_ GoogleTestFailureException : public ::std::runtime_error {

显然,你或所使用的工具(如VSCode、CTest、CMake)会在失败条件处停止调试器:

  1. 运行 ctest --test-arg "--gtest_throw_on_failure"
  2. 或者,export GTEST_THROW_ON_FAILURE=1
  3. 或者,set_tests_properties(your_test_target_name PROPERTIES ENVIRONMENT "GTEST_THROW_ON_FAILURE=1")
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章