gtest 有没有办法让内联/测试用例甚至测试超时。
例如我想做这样的事情:
EXPECT_TIMEOUT(5 秒, myFunction());
我发现这个问题 googletest 从 2010 年 12 月 9 日开始问题为“Type:Enhancement”。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.
}
现在我有了失败的测试,我可以编写修复测试的代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)