Test execution time in unit test class via maven surefire-report in one file in a generalized format

Can someone tell me how can I get the time spent by each unit test in the unit test class in one file via maven-surefire ? I saw that my target/surefire-report has files for each test. Basically I am looking for a single file with total execution time. If possible, also sort the result by the time taken to complete each test.

I am using maven 3.5 and surefire-plugin 2.4.2 on macOSX 10.12.6.

+5
source share
3 answers

Currently maven-surefire-plugin does not allow you to do this. It writes all results to separate files. You can create a request function in problem tracking if you feel that it is a missing function.

However, you can use some Linux commands to convert the output to what you need. Here are a few commands that turn individual XML files into a single file that looks the way 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 : Numerical sorting has problems with varying numbers of fractions of a digit. Use the awk version below to solve this problem.


The same can be done with awk 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 must run these commands from the toplevel directory of your maven project after reporting accuracy.

If you have a multi-module project, use 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 time consumed.

+2
source

You can use surefire-report-plugin to aggregate the results (but it will not be sorted anyway)

 mvn surefire-report:report -DlinkXRef=false -Daggregate=true 

or

 mvn surefire-report:report-only -DlinkXRef=false -Daggregate=true 

if you have already created a project using the surefire plugin.

This will create a surefire-report.html in the root target directory, where you can find time statistics for each module and each test suite.

0
source

I wrote a small bash script with Martin Höller that can help someone:

 #!/bin/bash # This script goes through tests results in `target/surefire-reports` and sorts the results by the # amount of time they took. This is helpful to identify slow tests. set -e # Make sure the surefire folder exists if [ ! -d "target/surefire-reports" ]; then echo "The folder 'target/surefire-reports' doesn't exists. Please run tests before using this script." exit 1 fi # Go through the surefire test reports and sort tests by time taken. Source for this snippet: # https://stackoverflow.com/questions/45854277/execution-time-of-tests-in-unit-test-class-via-maven-surefire-report-in-a-single/45859700#45859700 grep -h testcase target/surefire-reports/TEST-*.xml | awk -F '"' '{print $4 "#" $2 "() - " $6 "ms" }' | sort -rn -k 3 
0
source

Source: https://habr.com/ru/post/1271175/


All Articles