Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execution time of tests in unit test class via maven surefire-report in a single file in summarized format

Can anyone let me know how can I get the time taken by each of the unit tests in a unit test class in a single file via maven-surefire? I have seen my target/surefire-report it has files for each test. Basically I am looking for a single file with all the execution times summarized. If possible also sort the result by execution time of each test.

I am using maven 3.5 & surefire-plugin 2.4.2 on MacOSX 10.12.6.

like image 479
tuk Avatar asked Aug 24 '17 06:08

tuk


People also ask

What is Maven surefire report?

The Surefire Report Plugin parses the generated TEST-*. xml files under ${basedir}/target/surefire-reports and renders them using DOXIA, which creates the web interface version of the test results.

How do you run a single unit test in Maven?

If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”. For instance, we can pass -Dtest=”TheFirstUnitTest” to the mvn command to execute the TheFirstUnitTest class only: $ mvn test -Dtest="TheFirstUnitTest" ...

What is a typical Maven goal command to execute unit tests?

We can run our unit tests with Maven by using the command: mvn clean test.


1 Answers

The maven-surefire-plugin currently doesn't let you do this. It writes all the results in separate files. You could create a feature-request in its issue tracker, if you feel like this is a missing feature.

However you can use some Linux commands to convert the output to what you need. Here are some commands that turn the separate XML files into a single file that looks like what you want:

grep testcase target/surefire-reports/TEST-*.xml |
  sed 's/.* name="\(.*\)" classname="\(.*\)" time="\(.*\)".*/\2#\1() - \3ms/g' |
  sort -n -k 3 > output.txt

Update: Numeric sorting has problems with varying number of fraction digits. Use awk version below to solve this.


The same thing could be done with awk a bit shorter and less cryptic:

grep -h testcase target/surefire-reports/TEST-*.xml |
  awk -F '"' '{printf("%s#%s() - %.3fms\n", $4, $2, $6); }' |
  sort -n -k 3 > output.txt

You have to execute these commands from the toplevel directory of your maven project after the surefire-reports were generated.

If you have multi-module project, use this instead:

find . -name TEST-*.xml -exec grep -h testcase {} \; |
  awk -F '"' '{printf("%s#%s() - %.3fms\n", $4, $2, $6); }' |
  sort -n -k 3 > output.txt

The resulting file is output.txt and contains lines of the following format:

<classname>#<methodname>() - <time>ms

The result is sorted by consumed time.

like image 73
Martin Höller Avatar answered Nov 08 '22 11:11

Martin Höller