Mean Variance
Mean Variance

Reputation: 11

Capturing timing data from Junit tests on Eclipse

Running on Eclipse Galileo (3.5), I noticed my tests show the timing of each test run. Is there a way to capture and this information? Is there an API or is it stored in a result file?

Screenshot

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

Upvotes: 1

Views: 491

Answers (1)

卢声远 Shengyuan Lu
卢声远 Shengyuan Lu

Reputation: 32004

We could use Rule to get time durations for each test method:

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

Upvotes: 2

Related Questions