Using Maven, how do I run specific tests?

Unit TestingMaven

Unit Testing Problem Overview


I have thousands of unit tests in my project, and I'd like to choose one or a couple of them to run from the command line. What's the command to do that?

Unit Testing Solutions


Solution 1 - Unit Testing

You can run all the tests in a class, by passing the -Dtest=<class> flag to Maven:

mvn clean test -Dtest=xxxxTest

Since Surefire 2.8, you can also run an individual test, say a method testA within your unit tests, using the same flag:

mvn clean test -Dtest=xxxxTest#testA

More examples for running multiple tests, by name pattern or name lists, can be found in the Maven Surefire documentation > Running a Single Test.

Solution 2 - Unit Testing

Please read this piece of the maven surefire plugin manual. Basically you can do the following:

mvn -Dtest=*PerformanceTest clean test 

Which only runs all the test classes ending in PerformanceTest.

Solution 3 - Unit Testing

+1 to the above answers, and... make sure you're in the same directory of the module where the test you're trying to run lives. I ran into this issue, because I normally work in multi-module projects and run mvn clean install from the root of the project... but for this use case, you need to cd into the module of the test you're trying to run.

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
Questionuser84592View Question on Stackoverflow
Solution 1 - Unit TestingVineet ReynoldsView Answer on Stackoverflow
Solution 2 - Unit TestingHiery NomusView Answer on Stackoverflow
Solution 3 - Unit TestingJake TorontoView Answer on Stackoverflow