maven :: run only single test in multi-module project

Unit TestingMavenContinuous Integration

Unit Testing Problem Overview


Is there any way to provide some command-line argument in order to skip all tests but one on some module? So I will not need to change pom.xml every time I will need to run another test?

For example, I want to create build configuration on TeamCity, and provide command-line arguments to run only single test in some module. Next time I will need to change it and run another test, and so on.

Perhaps it is not how CI is intended to be used, but still.

Unit Testing Solutions


Solution 1 - Unit Testing

I assume you've read the docs about running a single test under surefire? What they don't tell you is how to do that in a sub-module:

mvn test -Dtest=testname -pl subproject

Where subproject is the project containing that test. From the mvn man page:

> -pl,--projects arg Comma-delimited list of specified reactor projects to build instead of all projects. A project can be specified by [groupId]:artifactId or by its relative path.

Solution 2 - Unit Testing

Other answers I see are not fully complete, for projects that depend on other sub-modules to be built. One option is to run mvn install to have the required jars to be installed into ~/.m2/..., but that option is not very "clean".

Following command will build the sub-modules, and run only the test class that is specified. This is to be run at parent module level. Also, no need to specify sub-module name.

mvn test -DfailIfNoTests=false -Dtest={test_class_name} -am

As an aside, this can also be mvn clean test -Dfa...... I have a habit of always running clean when running tests.

References..
-am will make all the other sub-modules.
-DfailIfNoTests=false does not fail the entire process since we are not intending to run tests in other modules.
-pl option is not needed since -am is already building everything

Solution 3 - Unit Testing

In case the module to be tested depends on other projects, solution works by changing commands as:

mvn test -DfailIfNoTests=false -Dtest=testname -pl subproject

Solution 4 - Unit Testing

FWIW, if you have a multi-module project, you can run all tests with this command at parent directory.

mvn test -pl subproject

And the subproject's name can be found by running the following command, usually in the form of group-id:artifact-id.

mvn help:active-profiles

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjdevelopView Question on Stackoverflow
Solution 1 - Unit TestingNick GernerView Answer on Stackoverflow
Solution 2 - Unit TestingDebosmit RayView Answer on Stackoverflow
Solution 3 - Unit TestingLalit KumarView Answer on Stackoverflow
Solution 4 - Unit TestingMichaelZView Answer on Stackoverflow