Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record time it takes JUnit tests to run

I would like to record how long it takes my JUnit test to run programmatically. I have a large number of tests in various test classes, and I would like to find out how long each individual test method takes to run.

I can change the inheritance structure or annotate methods differently, but I would like to avoid having to add code within the test method itself and also within the before/after methods which are used to setup test business logic.

like image 308
oneself Avatar asked Jul 09 '13 15:07

oneself


Video Answer


2 Answers

Try to use @Before and @After. A method annotated with @Before or @After runs before or after the test.

    @Before
    public void start() {
        start = System.currentTimeMillis();
    }

    @After
    public void end() {
        System.out.println(System.currentTimeMillis() - start);
    }
like image 42
Heejin Avatar answered Sep 27 '22 23:09

Heejin


You could use the JUnit StopWatch rule and override its methods as provided in the JUnit API documentation and have the time printed to console or log file for each test just by including one line of code in each of your individual test case class.

  1. Create your Customer StopWatch class (Sample provided)

    import java.util.concurrent.TimeUnit;
    import org.junit.AssumptionViolatedException;
    import org.junit.rules.Stopwatch;
    import org.junit.runner.Description;
    
    public class MyJUnitStopWatch extends Stopwatch{
    
    private static void logInfo(Description description, String status, long nanos) {
        String testName = description.getMethodName();
        System.out.println(String.format("Test %s %s, spent %d microseconds",
                                  testName, status, TimeUnit.NANOSECONDS.toMicros(nanos)));
    }
    
     @Override
       protected void succeeded(long nanos, Description description) {
           logInfo(description, "succeeded", nanos);
       }
    
       @Override
       protected void failed(long nanos, Throwable e, Description description) {
           logInfo(description, "failed", nanos);
       }
    
       @Override
       protected void skipped(long nanos, AssumptionViolatedException e, Description description) {
           logInfo(description, "skipped", nanos);
       }
    
       @Override
       protected void finished(long nanos, Description description) {
           logInfo(description, "finished", nanos);
       }    
    }
    
  2. Have a ParentTestClass created with that line and each of your test class inherit the parent test class:

    public class ParentTestCase {
    
    
    @Rule
    public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
    
    }
    

Child classes inherits parent. No other change in Child classes or before or after methods.

public class TestUniqueCharacterString extends ParentTestCase {    

    private String uniqueChars = null;

    @Before
    public void before(){
        uniqueChars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnop";
    }

    @Test
    public void testMyUniqueCharacterMethod(){


        UniqueCharacteString.doesStringHaveUniqueCharacters(uniqueChars);
    }

Or

  1. Include this line in each of your Test class

    @Rule
    public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
    

    Sample Test Class:

    import org.junit.After;
    import org.junit.Before;
    import org.junit.Rule;
    import org.junit.Test;
    
    
    
    public class TestUniqueCharacterString {    
    
    @Rule
    public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
    
    private String uniqueChars = null;
    
    @Before
    public void before(){
        uniqueChars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnop";
    }
    
    @Test
    public void testMyUniqueCharacterMethod(){
    
    
        UniqueCharacteString.doesStringHaveUniqueCharacters(uniqueChars);
    }
    
    @Test
    public void testGoodIsUniqueMethod(){
            UniqueCharacteString.isUniqueCharacs(uniqueChars);
    }
    
    @Test
    public void testGoodIsUniqueMethodWithoutArray(){
        UniqueCharacteString.isUniqueCharsWithoutArray(uniqueChars);
    }
    
    @After
    public void after(){
        uniqueChars = "";
    }    
     }
    

JUnit API reference:

http://junit.org/apidocs/org/junit/rules/Stopwatch.html

Sample Output

Test testMyUniqueCharacterMethod succeeded, spent 3250 microseconds
Test testMyUniqueCharacterMethod finished, spent 3250 microseconds
Test testGoodIsUniqueMethod succeeded, spent 70 microseconds
Test testGoodIsUniqueMethod finished, spent 70 microseconds
Test testGoodIsUniqueMethodWithoutArray succeeded, spent 54 microseconds
Test testGoodIsUniqueMethodWithoutArray finished, spent 54 microseconds

It will also show time for failed and skipped test cases.

like image 94
javaJavaJava Avatar answered Sep 28 '22 00:09

javaJavaJava