How to run all tests in a particular package with Maven?
I can find in the Maven docs where it shows how to run:
- A single test
- All tests in a single test class
- All tests in classes matching a particular pattern
But how to run all the tests in a package? Is this possible?
I would prefer solutions that don't require modifying the pom.xml
or code.
You could use a pattern as well, for example
mvn '-Dtest=de.mypackage.*Test' test
runs all tests in classes from package de.mypackage ending on *Test
.
[update 2017/12/18]:
Since this became the accepted answer, here's some further information:
- Maven uses the Maven Surefire plugin to execute tests.
-
The syntax used above (qualified package name) requires Surefire version
2.19.1
or higher! Earlier versions require the use of path expressions, for examplemvn -Dtest="de/mypackage/*Test" test
I'm using quotes (` or ") to prevent the shell from performing pathname expansion, Maven doesn't require any quotes.
-
A single test method can be exuted using the following syntax
mvn -Dtest=MyUnitTest#testMethod test
-
All tests from subpackages may be includes as well, in order to execute all tests in or beneath package
de.mypackage.sub
execute:mvn -Dtest="de/mypackage/sub/**" test
or with Surefire
2.19.1
or highermvn -Dtest="de.mypackage.sub.**" test
There are further possibilities like using regular expressions, see the official documentation of running a single test.
AFAIK there are no command line parameter for surefire:test to run tests in a specific package.
I use a configuration variable to achieve the same effect. A fragment of my pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<configuration>
<includes>
<include>**/${testGroup}/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Now if I want to run tests in a package named "com.example", I use the following command:
mvn test -DtestGroup=com/example