Record time it takes JUnit tests to run

后端 未结 7 1327
无人共我
无人共我 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: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.

提交回复
热议问题