Timeout for tests

如果想把timeout太久的测试自动标记为失败,有两种方法:

1.在 @Test里加上 Timeout 参数

定义"timeout=1000“的话,如果超过1000 毫秒,failure会被一个抛出的异常触发。

import static org.junit.Assert.*; 
import org.junit.Test; 

public class TimeoutTest { 

@Test(timeout=1000) 
public void test(){ 
assertTrue("it should be true",true); 
} 
} 

2.定义Timeout 规则(应用于整个测试类)

import org.junit.Rule; 
import org.junit.Test; 
import org.junit.rules.Timeout; 

public class Timeout2 { 
public static String log; 

@Rule 
public Timeout globalTimeout=new Timeout(10000); 

@Test 
public void test01(){ 
log +="ran1"; 
for(;;){} 
} 

@Test 
public void test02(){ 
log+="ran2"; 
for(;;){} 
} 
}
原文地址:https://www.cnblogs.com/miniren/p/4638502.html