简体   繁体   English

如何测试在 googletest 中是否未调用 std::terminate

[英]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.

相关问题 我可以在 googletest 中测试死锁吗? - Can I test deadlock in googletest? 我该如何解决这个“抛出‘std::out_of_range’实例后调用的终止”错误? - how can i fix this "terminate called after throwing an instance of 'std::out_of_range" errors? 如何解决此错误? 抛出&#39;std :: bad_alloc&#39;what()实例之后调用终止终止:std :: bad_alloc - How I can fix this error? terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc 我可以将参数传递给 googletest 测试函数吗 - Can I pass parameters to googletest test function 如何测试分配器是否使用std :: allocate进行内存分配? - How can I test whether an allocator is using std::allocate for memory allocation? 在'std::invalid_argument' what() 之后调用终止:stoi。 这是从我的实际代码中导出的测试代码。 我可以忍受死亡和案件 - terminate called after 'std::invalid_argument' what(): stoi. This is a test code derived from my actual code. I can stoi death and case GoogleTest:如何跳过测试? - GoogleTest: How to skip a test? 回归测试如何证明VirtualAlloc是否被调用? - How can a regression test prove whether VirtualAlloc was called? 当我尝试终止 std::thread 时调用 abort() - abort() is called when I try to terminate a std::thread 如何禁用Googletest(gtest)参数化测试? - How do I disable a Googletest (gtest) parametrized test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM