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

Viewed 3497

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.

5 Answers

Junit5 also has a @Timout annotation.

There is also an assertTimeout and assertTimeoutPreemptively (see documentation).

Finally, the JUnit 4 Stopwatch functionality is not available in JUnit 5. Use TimeExtension as already mentioned. However this extension is not (yet?) part of the distribution (see issue 343).

If you are using Sonarqube, you can find time duration of each test and total of all test sorted by time

test time sorted

all test

There is nothing special that needs to be done to get the execution time for a test case in JUnit 5 - Jupiter.

The time is right there in the XML report.

On the 'testCase' element, there is a property 'time', which gives the time for execution of the test case. Below is an example. The times are in seconds

<testcase name="checkZKConnectivityWithAuth" 
          classname="test.zk.ZookeeperConnectionProviderTests" 
          time="7.177"/>

<testcase name="checkConfigRetrieval" 
          classname="test.zk.ZookeeperConnectionProviderTests" 
          time="0.213"/>

<testcase name="checkConfigInsertion" 
          classname="test.zk.ZookeeperConnectionProviderTests" 
          time="0.255"/>
Related