74

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?

Mike Partridge
  • 5,128
  • 8
  • 35
  • 47
user84592
  • 4,750
  • 11
  • 55
  • 91

3 Answers3

112

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.

t0r0X
  • 4,212
  • 1
  • 38
  • 34
Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
  • 1
    appears from http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html that you can also optionally also specifiy both test class *and* method name as well, is that right? – rogerdpack Feb 25 '13 at 20:07
  • How about a list of classes? ie. 5 or 6 Test classes? – Daniel Patrick Oct 03 '16 at 13:01
  • 1
    Although the Maven Surefire documentation title is 'Running a Single Test', it also explains how to run multiple tests. For example, `mvn -Dtest=TestSquare,TestCi*le test` – sudeep Aug 29 '17 at 19:08
31

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.

Hiery Nomus
  • 17,429
  • 2
  • 41
  • 37
3

+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.

Jake Toronto
  • 3,524
  • 2
  • 23
  • 27