googletest中的测试用例超时 [英] Time out for test cases in googletest

查看:222
本文介绍了googletest中的测试用例超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gtest中是否有一种方法可以使内联/测试用例甚至测试超时. 例如,我想做类似的事情: EXPECT_TIMEOUT(5秒,myFunction());

Is there a way in gtest to have a timeout for inline/test cases or even tests. For example I would like to do something like: EXPECT_TIMEOUT(5 seconds, myFunction());

我从2010年12月9日起发现此问题googletest问题为类型:增强". https://code.google.com/p/googletest/issues/detail ?id = 348

I found this issue googletest issues as 'Type:Enhancement' from Dec 09 2010. https://code.google.com/p/googletest/issues/detail?id=348

看起来这篇文章没有gtest方法. 我可能不是第一个尝试找到解决方法的人.

Looks like there is no gtest way from this post. I am probably not the first to trying to figure out a way for this.

我能想到的唯一方法是让子线程运行该函数,如果子线程不返回该子函数,则该子线程运行该函数. 时间限制,父线程将杀死它并显示超时错误.

The only way I can think is to make a child thread run the function, and if it does not return by the time limit the parent thread will kill it and show timeout error.

有没有不需要使用线程的方式? 还是其他方式?

Is there any way where you don't have to use threads? Or any other ways?

推荐答案

我刚遇到这种情况.

我想为我的反应堆添加一个失败的测试.反应堆永远不会完工. (它必须首先失败).但是我不希望测试永远运行.

I wanted to add a failing test for my reactor. The reactor never finishes. (it has to fail first). But I don't want the test to run forever.

我跟踪了您的链接,但仍然不满意.因此,我决定使用某些C ++ 14功能,并使它相对简单.

I followed your link but still not joy there. So I decided to use some of the C++14 features and it makes it relatively simple.

但是我实现了这样的超时:

But I implemented the timeout like this:

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.
}

现在我的测试失败了,我可以编写修复测试的代码了.

Now that I have my failing test I can write the code that fixes the test.

这篇关于googletest中的测试用例超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆