Computing the total test execution time of Maven Surefire

When working with Maven projects, the Surefire plugin is commonly used to execute tests, but it lacks a built-in feature to display the total execution time across all test suites. This can be particularly important when monitoring performance trends in larger projects with many test classes.

Maven’s Surefire plugin reports execution time for individual test classes but doesn’t provide an aggregated view of the total test execution time. The reports are generated in the “target/surefire-reports” folder, both as text files and XML files.

Here’s a shell script that parses the XML report files generated by Surefire and calculates the total execution time. The script is compatible with both Linux and macOS environments.

These are the steps:

  1. Look for lines containing <testsuite> tags
  2. For each matching line, loops through all fields (words) in the line
  3. Find fields that start with time=
  4. Uses gsub() to extract just the numeric value by removing the time=" prefix and the " suffix
  5. Add the extracted value to the running total
  6. Format the output in the same way, with the total time in seconds

To make this script run automatically after your tests, you can integrate it into your Maven build process using the exec-maven-plugin:

The above snippet assumes the script is in a file “report-total-time.sh” in the same directory as the POM. Otherwise, you’ll have to adjust the argument accordingly.

When your tests complete, you’ll see output similar to:

That’s all!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.