我怎样才能 运行 google 仅在调试中进行死亡测试?

How can I run google death tests in debug only?

我们有一系列死亡测试来检查特定调试 asserts 是否触发。例如,我们构造这样的东西:

LockManager::LockManager(size_t numManagedLocks) :
    _numManagedLocks(numManagedLocks)
{
    assert(_numManagedLocks <= MAX_MANAGABLE_LOCKS &&
        "Attempting to manage more than the max possible locks.");

我们对其失败进行了测试:

EXPECT_DEATH(LockManager sutLockManager(constants::MAX_NUMBER_LOCKS + 1), 
    "Attempting to manage more than the max possible locks.");

由于断言仅在调试中编译,因此当组件在发布中构建时,这些测试将失败。避免这种情况的最好方法是将 EXPECT_DEATH 测试包装在 DEBUG 检测宏中:

#ifndef NDEBUG
     // DEATH TESTS
#endif

或者是否有更好的特定于 Google 测试的方法?

由于 assert() 宏使用预处理器逻辑,因此解决方案也应该在此级别上 - 通过条件编译。 您可以使用 GoogleTest 特定的 DISABLED_ 语法(参见 Temporarily Disabling Tests)并编写类似

的内容
#ifdef _DEBUG
#define DEBUG_TEST_ 
#else
#define DEBUG_TEST_ DISABLED_
#endif 

你原来的建议看起来也不错,不过我还是直接写条件为好:

#ifdef _DEBUG 
 ...

我们生成了一个工作宏来代替完整的死亡测试或仅出现在其他测试中的ASSERT_DEATH

#if defined _DEBUG

    #define DEBUG_ONLY_TEST_F(test_fixture, test_name) \
        TEST_F(test_fixture, test_name)
    #define DEBUG_ONLY_ASSERT_DEATH(statement, regex) \
        ASSERT_DEATH(statement, regex)

#else

    #define DEBUG_ONLY_TEST_F(test_fixture, test_name) \
        TEST_F(test_fixture, DISABLED_ ## test_name)
    #define DEBUG_ONLY_ASSERT_DEATH(statement, regex) \
        std::cout << "WARNING: " << #statement << " test in " << __FUNCTION__ << " disabled becuase it uses assert and fails in release.\n";

#endif

当然,我们需要覆盖我们使用的任何其他测试类型(例如 TEST_PEXPECT_DEATH),但这应该不是什么大问题。