当 TC 超时时,我如何捕获超时?例如,在下面的 TC 中,我认为 catch block 会在超时后执行,但事实并非如此。我想在超时后进行相同的处理,就好像 TC 异常失败一样。
@Rule
public Timeout globalTimeout = new Timeout(3600000);
@Test
public void tc1 throws Exception {
try {
//do stuff
}
catch (Throwable t) {
// take screenshot, output results
}
}
最佳答案
正如文档所说:
Each test is run in a new thread. If the specified timeout elapses before the test completes, its execution is interrupted via
Thread.interrupt()
. This happens in interruptable I/O and locks, and methods in Object and Thread throwingInterruptedException
.
这意味着 InterruptedException
在超时时被抛出。识别 Throwable
类型并处理日志记录情况,如下所示:
@Test
public void tc1 throws Exception {
try {
//do stuff
}
catch (Throwable t) {
// take screenshot, output results
if(t instanceof InterruptedException){
// InterruptedException logging case
}
}
}
关于java - 捕获超时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30212543/