[英]How can I test whether std::terminate is NOT called in googletest
I have a custom assertion like this:我有一个这样的自定义断言:
#define MY_ASSERT(condition, message) \
do { \
if (!(condition)) { \
std::cerr << "Assertion `" #condition "` failed in " << __FILE__ \
<< " line " << __LINE__ << ": " << message << std::endl; \
std::terminate(); \
} \
} while (false)
I know that I can use Death Tests to check if assertions are called.我知道我可以使用死亡测试来检查断言是否被调用。 However, I want to check whether the assertion does not actually happen.但是,我想检查断言是否实际上没有发生。 I want the specific test to actually FAIL instead of the whole application exit with code 3 because assertion failed.我希望特定测试实际上失败,而不是使用代码 3 退出整个应用程序,因为断言失败。 Here is the test that I have written:这是我写的测试:
TEST_F(RenderGraphDeathTest, BuildOnlyCalledOnce) {
liquid::RenderGraph graph;
graph.addPass<EmptyScope>(
"A", [](auto &builder, auto &scope) { builder.write("a-b", {}); },
noopExecutor);
graph.compile();
// Looking for something like EXPECT_NOT_DEATH
graph.compile();
}
Set a custom terminate handler in the test start.在测试开始时设置自定义终止处理程序。 The default handler is std::abort
.默认处理程序是std::abort
。 See std::terminate_handler .见std::terminate_handler 。
I ended up forcefully exiting and checking the exit status code in a Death Test:我最终强行退出并在死亡测试中检查退出状态代码:
TEST_F(RenderGraphDeathTest, BuildOnlyCalledOnce) {
liquid::RenderGraph graph;
graph.addPass<EmptyScope>(
"A", [](auto &builder, auto &scope) { builder.write("a-b", {}); },
noopExecutor);
ASSERT_EXIT({
graph.compile();
graph.compile();
// If no assertion is happening,
// this test will exit with code 0,
// otherwise, it will exit with the
// SIGABORT due to std::abort
// default terminate handler
exit(0);
}, ::testing::ExitedWithCode(0), ".*");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.