Record time it takes JUnit tests to run

后端 未结 7 1305
无人共我
无人共我 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条回答
  •  闹比i
    闹比i (楼主)
    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

提交回复
热议问题