如何仅在调试中运行谷歌死亡测试?

How can I run google death tests in debug only?

本文关键字:测试 谷歌 运行 何仅 调试      更新时间:2023-10-16

我们有一系列的死亡测试来检查特定的调试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.");

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

#ifndef NDEBUG
// DEATH TESTS
#endif

或者有没有一种更好且特定于谷歌测试的方法?

由于 assert(( 宏使用预处理器逻辑,因此解决方案也应该在此级别 - 通过条件编译。 您可以使用GoogleTest特定的DISABLED_语法(请参阅暂时禁用测试(并编写类似以下内容

#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(,但这应该不是一个大问题。

我认为GTest现在有一个解决方案: EXPECT_DEBUG_DEATH