How to run all tests in a particular package with Maven?

JavaMavenJunit

Java Problem Overview


I can find in the Maven docs where it shows how to run:

  1. A single test
  2. All tests in a single test class
  3. 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.

Java Solutions


Solution 1 - Java

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 example

      mvn -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 higher

    mvn -Dtest="de.mypackage.sub.**" test

There are further possibilities like using regular expressions, see the official documentation of running a single test.

Solution 2 - Java

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

Solution 3 - Java

You can take reference from the following scenarios:

(1) Skip all test execution during maven build

mvn package -DskipTests

(2) Execute All test cases

mvn test

(3) Execute specific test group

mvn test -DincludeGroups=TestGroup1,TestGroup2

(4) Exclude specific test group during execution

mvn test -DexcludeGroups=TestGroup3,TestGroup4

(5) Execute specific package testcases

mvn test -Dtest="test.java.com.service.map.**"

(6) Execute specific test

mvn test -Dtest=Test1,Test2

I hope it will be more helpful to you with a different combination of execution.

Solution 4 - Java

mvn test -Dtest=com.myrootpackage.myfirstlevelpackage.*

...and if you want to include all sub packages:

mvn test -Dtest=com.myrootpackage.myfirstlevelpackage.**.*

Solution 5 - Java

The pom.xml version of O Badr's answer:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <includes>
                 <!-- includes all tests, but not in subpackages -->
                        <include>**/package1/*</include>   
                 <!-- includes all tests, AND in subpackages -->
                        <include>**/package2/**</include>
                    </includes>
                </configuration>
            </plugin>

        </plugins>
    </build>

Solution 6 - Java

Had the same problem (how to run all tests in a particular package) and tried many ways, two commands that worked were:

mvn -Dtest=com.package.my.package.** test

mvn -Dtest=com.package.my.package.**.* test

Solution 7 - Java

It may not be the best answer, but you could run a single TestSuite (which is a Test) which includes all the test you want in one package.

Solution 8 - Java

It can also be done by

mvn clean test -Dtest="**/service/**/*.java"

eg: I am in my /home/repository and I want to run tests on /home/repository/service folder only

Here service folder is inside my current folder.

Solution 9 - Java

You can mention packages in your pom.xml through Surefire-plugin

<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-plugin</artifactId>
			<version>2.19.1</version>
			<configuration>
				<includes>
					<include>com.mycom.mydep.mypak.*, com.mycom.mydep.mypak2.*</include>
				</includes>
			</configuration>
		</plugin>

Please note that JUnit version should be greater than 4.8. and version of surefire should be 2.19.1 Find more details here

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
QuestionEric WilsonView Question on Stackoverflow
Solution 1 - JavaRobinView Answer on Stackoverflow
Solution 2 - JavaNulldeviceView Answer on Stackoverflow
Solution 3 - JavaRadadiya NikunjView Answer on Stackoverflow
Solution 4 - JavaO.BadrView Answer on Stackoverflow
Solution 5 - JavaAndrejsView Answer on Stackoverflow
Solution 6 - JavaKristinaView Answer on Stackoverflow
Solution 7 - JavaTristanView Answer on Stackoverflow
Solution 8 - JavaswapyonubuntuView Answer on Stackoverflow
Solution 9 - JavaVikkyView Answer on Stackoverflow