Record time it takes JUnit tests to run

后端 未结 7 1285
无人共我
无人共我 2021-02-04 11:52

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 ea

相关标签:
7条回答
  • 2021-02-04 12:28

    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);
        }
    
    0 讨论(0)
  • 2021-02-04 12:30

    You can also create a @Rule and instantiate the TestWatcher class. This is what worked for me. This is being defined in a class that extends TestCase.

    public class CompositeWithTeardownDBUnitTest extends DBTestCase {
    

    DBTestCase extends TestCase

    code snippet in CompositeWithTeardownDBUnitTest

    @Rule
    public TestRule watcher = new TestWatcher() {
    
        protected void starting(Description description) {
            timeStart = System.currentTimeMillis();
            cal = Calendar.getInstance();
            System.out
                    .println("===========================================================================");
            System.out.println("Test: " + description.getMethodName());
            System.out.println("Start Time: " + dateFormat.format(cal.getTime()));
            System.out
                    .println("===========================================================================");
        }
    
        protected void finished(Description description) {
            timeEnd = System.currentTimeMillis();
            double seconds = (timeEnd-timeStart)/1000.0;
            System.out
            .println("\n===========================================================================");
            System.out
            .println("Test completed - ran in: "+new DecimalFormat("0.000").format(seconds)+" sec");
            System.out
            .println("===========================================================================\n");
    
        }
    };
    

    And the JUnit test classes just extend this class CompositeWithTeardownDBUnitTest.

    0 讨论(0)
  • 2021-02-04 12:32

    If you use @Before and @After annotations and note the junit testcase start and endtime. Then finding the difference of two timestamps should give you the testcase execution time. Something like this:

     public class Example {
    
        long startTime;
        long endTime;
    
        @Before public void recordStartTime() {
            startTime = System.currentTimeMillis();
        }
        @Test public void testSomething() {
              //test method
        }
        @After public void recordEndAndExecutionTime() {
            endTime = System.currentTimeMillis();
            System.out.println("Last testcase exection time in millisecond : " + (endTime - startTime));
        }
     }
    
    0 讨论(0)
  • 2021-02-04 12:35

    In addition to existing answers, you can use a rule for test name along with Before and After methods to display method name on log. Like this:

    public class ImageSavingTest {
        @Rule
        public TestName name = new TestName();
    
        private long start;
    
        @Before
        public void start() {
            start = System.currentTimeMillis();
        }
    
        @After
        public void end() {
            System.out.println("Test " + name.getMethodName() + " took " + (System.currentTimeMillis() - start) + " ms");
        }
    
        @Test
        public void foobar() {
            // test code here
        }
    }
    

    Will output:

    Test foobar took 1828 ms

    0 讨论(0)
  • 2021-02-04 12:41

    You could create a JUnit Rule that would record the time between before/after calls. This rule could be used as an instance and/or class rule to get you the time for each individual test method as well as for each test class.

    0 讨论(0)
  • 2021-02-04 12:48

    Create your own TestWatcher implementation which catches every test method running. Using Guava Stopwatch you can measure time for each test:

    public class TimeTestWatcher extends TestWatcher {
        private Stopwatch stopwatch = Stopwatch.createUnstarted();
    
        protected void starting(Description description) {
            stopwatch.start();
        }
    
        protected void finished(Description description) {
            stopwatch.stop();
    
            String testName = description.getMethodName();
            long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
            System.out.println(String.format("Test %s took %d ms.", testName, elapsed));
        }
    };
    

    And then add JUnit @Rule annotation with your TimeTestWatcher for each test class:

    public class YourTest {
    
        @Rule
        public TimeTestWatcher watcher = new TimeTestWatcher();
    
        @Test
        public void testXXX() {}
    
        @Test
        public void testYYY() {}
    }
    
    0 讨论(0)
提交回复
热议问题