运行junit测试一定时间 [英] Run junit test certain amount of time

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

问题描述

我想测试一段时间,例如5秒钟,如果没有断言没有错误,则通过测试.注释可以吗?可以使用@Test(uptime=5000)之类的东西吗?

I want to test something for a while, say 5 seconds, and then pass the test if nothing wrong has been asserted. Is this possible with annotations? Can something like @Test(uptime=5000) be used?

推荐答案

问题编辑后的修订答案

从根本上说,您在这里测试错误的东西-什么都没有发生"是成功的标志,这很奇怪.

Fundamentally it feels like you're testing the wrong thing here - it seems very odd for "nothing happening" to be a sign of success.

如果您想证明算法可以运行一定时间而不会失败,那么我实际上会提取一个周期,然后编写类似以下内容的测试:

If you want to prove that your algorithm can run for a certain amount of time without failing, I would actually extract out a single cycle, then write a test of something like:

@Test
public void fineForFiveSeconds() {
    long start = System.nanoTime();
    long end = start + TimeUnit.SECONDS.toNanos(5);
    while (System.nanoTime < end()) {
        test.executeOneIteration();
    }
}

这样,您就没有一个单独的线程必须杀死正在工作的代码,等等.

This way you don't have a separate thread which has to kill the working code, etc.

原始答案

此答案写在之前,该问题表明超时是成功的标志,而不是失败的标志.

This answer was written before the question indicated that timing out was a sign of success, not failure.

我认为您只需要 timeout 属性中的@Test批注:

I think you just want the timeout attribute in the @Test annotation:

@Test(timeout = 5000)

附带文档:

(可选)以毫秒为单位指定超时,如果测试方法花费的时间超过该毫秒数,则会导致测试方法失败.

Optionally specify timeout in milliseconds to cause a test method to fail if it takes longer than that number of milliseconds.

这篇关于运行junit测试一定时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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