Search code examples
timeexecutionjunit5

Annotation to log the execution time of each test case in Junit5


I have a requirement to log the execution time for each test case. I don't want to use a custom implementation. I want to know whether there is an annotation available for this in junit5

Btw, I know about Stopwatch or Junit4 @Timeout.


Solution

  • After a look through into junit5 documentation, I found this sample TimeExtension

    This snippet is from their docs:

    @ExtendWith(TimingExtension.class)
    class TimingExtensionTests {
    
      @Test
      void sleep20ms() throws Exception {
         Thread.sleep(20);
      }
    
      @Test
      void sleep50ms() throws Exception {
        Thread.sleep(50);
      }
    
    }
    

    EDIT: Source code for TimingExtension