如何仅在调试中运行谷歌死亡测试?
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.");
由于 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_P
或EXPECT_DEATH
(,但这应该不是一个大问题。
我认为GTest现在有一个解决方案: EXPECT_DEBUG_DEATH
相关文章:
- 谷歌测试中的期望值存储在哪里
- 更正 CMakeList.txt 用于谷歌测试的文件?
- 如何让谷歌测试正常运行。测试总是失败。(它不会编译)
- 谷歌测试方法与流
- _mm256_load_ps调试模式下导致谷歌/基准测试的分段错误
- 谷歌测试:模板模板的笛卡尔乘积的类型列表与模板
- C++ 谷歌测试除以零
- 如何使用谷歌基准测试对自定义界面进行基准测试
- 谷歌基准测试,如何只调用一次代码?
- 多定义C ++ / C谷歌测试
- 使用谷歌测试进行测试时出现分段错误
- 如何使用谷歌测试检查两个枚举类元素的相等性?
- 谷歌测试集ArgReferee with vector<unique_ptr>
- 谷歌基准测试结果中显示的时间没有意义
- 如何测试我的谷歌身份验证器实现?
- 谷歌测试编译错误 Os X:函数式转换从"int"到"internal::FloatingEq2Matcher"的模糊转换<float>
- 谷歌测试发现在macOSX上添加并运行了我的所有测试两次
- 在谷歌测试中创建模拟类的向量
- 为什么在谷歌测试中调用"mkdtemp()"时失败
- 多次调用单个测试-谷歌测试