Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list the slowest JUnit tests in a multi-module Maven build

How can I list the slowest JUnit tests in a multi-module Maven build?

This should be accross all modules.

A Hudson/Jenkins solution could also do.

like image 710
Axel Fontaine Avatar asked Feb 23 '11 17:02

Axel Fontaine


People also ask

Does Maven run JUnit tests in parallel?

Maven Dependencies In a nutshell, Surefire provides two ways of executing tests in parallel: Multithreading inside a single JVM process. Forking multiple JVM processes.

Do JUnit tests run concurrently?

Once parallel test execution property is enabled, the JUnit Jupiter engine will execute tests in parallel according to the provided configuration with declared synchronization mechanisms.

Does mvn package run unit tests?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests.


1 Answers

Disclaimer: I truly apologize for my bash solution, although it works and fits in one line :-). If you are impatient, go to the bottom.

First we need to find all TEST-*.xml files produced by maven-surefire-plugin. Run this after mvn test in the root directory of your project to discover test results in all submodules:

$ find . -iname "TEST-*.xml"

Fortunately the format of these files is pretty straightforward, a simple grep and we have what we need:

$ grep -h "<testcase" `find . -iname "TEST-*.xml"`

Now some sed magic to extract invocation time, test case class and method name:

$ sed 's/<testcase time="\(.*\)" classname="\(.*\)" name="\(.*\)".*/\1\t\2.\3/'

There's nothing more left just to sort the result and display longest running tests:

$ sort -rn | head

Promised one-liner:

$ grep -h "<testcase" `find . -iname "TEST-*.xml"` | sed 's/<testcase time="\(.*\)" classname="\(.*\)" name="\(.*\)".*/\1\t\2.\3/' | sort -rn | head

Amazingly, the results look reasonable (Activiti 5.1 multi-module code-base taken as an example):

3.029   org.activiti.examples.variables.jpa.JPAVariableTest.testStoreJPAEntityAsVariable
2.904   org.activiti.engine.test.forms.FormsTest.testTaskFormPropertyDefaultsAndFormRendering
1.594   org.activiti.engine.test.api.mgmt.ManagementServiceTest.testGetJobExceptionStacktrace
1.114   org.activiti.examples.variables.jpa.JPAVariableTest.testUpdateJPAEntityValues
1.006   org.activiti.engine.test.db.EngineRebootProcessDefinitionCacheTest.testStartProcessInstanceByIdAfterReboot
0       org.activiti.engine.test.pvm.PvmVariablesTest.testVariables
0       org.activiti.engine.test.pvm.PvmScopeWaitStateTest.testWaitStateScope
0       org.activiti.engine.test.pvm.PvmScopesAndConcurrencyTest.testConcurrentPathsGoingIntoScope
0       org.activiti.engine.test.pvm.PvmEventTest.testNestedActivitiesEventsOnTransitionEvents
0       org.activiti.engine.test.pvm.PvmEventTest.testEmbeddedSubProcessEvents
like image 100
Tomasz Nurkiewicz Avatar answered Oct 22 '22 14:10

Tomasz Nurkiewicz