在gtest中是否存在内联/测试用例甚至测试超时的方法.例如,我想做类似的事情:EXPECT_TIMEOUT(5秒,myFunction());
我发现这个问题googletest问题的"类型:增强"从09年12月2010年 https://code.google.com/p/googletest/issues/detail?id=348
看起来这篇文章没有最好的方法.我可能不是第一个试图找到解决方法的人.
我能想到的唯一方法是让子线程运行该函数,如果它没有按时间限制返回,则父线程将终止它并显示超时错误.
有没有什么方法你不必使用线程?还是其他任何方式?
我刚遇到这种情况。
我想为反应堆添加失败的测试。反应堆永远不会完工。(它必须首先失败)。但是我不希望测试永远运行。
我跟踪了您的链接,但在那里仍然不开心。因此,我决定使用某些C ++ 14功能,它使其相对简单。
但是我实现了这样的超时:
TEST(Init, run)
{
// Step 1 Set up my code to run.
ThorsAnvil::Async::Reactor reactor;
std::unique_ptr<ThorsAnvil::Async::Handler> handler(new TestHandler("test/data/input"));
ThorsAnvil::Async::HandlerId id = reactor.registerHandler(std::move(handler));
// Step 2
// Run the code async.
auto asyncFuture = std::async(
std::launch::async, [&reactor]() {
reactor.run(); // The TestHandler
// should call reactor.shutDown()
// when it is finished.
// if it does not then
// the test failed.
});
// Step 3
// DO your timeout test.
EXPECT_TRUE(asyncFuture.wait_for(std::chrono::milliseconds(5000)) != std::future_status::timeout);
// Step 4
// Clean up your resources.
reactor.shutDown(); // this will allow run() to exit.
// and the thread to die.
}
Run Code Online (Sandbox Code Playgroud)
现在我的测试失败了,我可以编写修复测试的代码了。
| 归档时间: |
|
| 查看次数: |
4533 次 |
| 最近记录: |