我想测试一些东西,比如 5 秒,如果断言没有错误,然后通过测试。这可能带有注释吗?可以使用 @Test(uptime=5000)
之类的东西吗?
最佳答案
编辑问题后修改答案
从根本上说,感觉就像您在这里测试了错误的东西 - “什么都没发生”是成功的标志似乎很奇怪。
如果你想证明你的算法可以运行一定的时间而不会失败,我实际上会提取一个循环,然后写一个类似这样的测试:
@Test
public void fineForFiveSeconds() {
long start = System.nanoTime();
long end = start + TimeUnit.SECONDS.toNanos(5);
while (System.nanoTime < end()) {
test.executeOneIteration();
}
}
这样你就没有一个必须终止工作代码等的单独线程。
原始答案
这个答案写在问题之前,表明超时是成功的标志,而不是失败。
我想你只是想要 timeout
@Test
注释中的属性:
@Test(timeout = 5000)
带有文档:
Optionally specify timeout in milliseconds to cause a test method to fail if it takes longer than that number of milliseconds.
关于java - 运行 junit 测试一定时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15262820/