答案 0 :(得分:2)
我们可以使用Rule来获取每种测试方法的持续时间:
class TimeConsumeRule implements MethodRule {
@Override
public Statement apply(final Statement base, final FrameworkMethod method, Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
long start = System.currentTimeMillis();
try {
base.evaluate();
} finally {
System.out.println(method.getName()+ " used "+ (System.currentTimeMillis() - start)+" ms;");
}
}
};
}
}
public class TimeConsume {
//Just declare customized Rule in your test case.
@Rule
public MethodRule rule = new TimeConsumeRule();
@Test
public void test1(){
//...
}
@Test
public void test2(){
//...
}
}