从Eclipse上的Junit测试中捕获时序数据

时间:2011-04-04 22:54:29

标签: eclipse junit

在Eclipse Galileo(3.5)上运行,我注意到我的测试显示了每次测试运行的时间。有没有办法捕获和这些信息?是API还是存储在结果文件中?

截图

enter image description here http://ge.tt/3ylDyiq

1 个答案:

答案 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(){ 
       //...
      }
}