Skip Unit Tests in Maven

To skip unit tests you can configure maven with surefire plugin to completely skip your unit tests or selectively skip your unit tests, based on criteria. We also explain the two options when disabling these unit tests: the difference about maven.test.skip and skipTests.

Skip Unit Tests in Maven with argument

You can skip unit tests via command line by executing the following command:

mvn install -DskipTests

If you absolutely must, you can also use the maven.test.skip property to skip compiling the tests. maven.test.skip is honoured by Surefire, Failsafe and the Compiler Plugin.

mvn install -Dmaven.test.skip=true

Difference between skipTests and maven.test.skip

  • maven.test.skip: disables both running the tests and compiling the tests.
  • skipTests: skips running tests, but still compile them.
Note: If you must disable your unit tests always prefer skipTests over maven.test.skip because otherwise your existing unit test code will not be compiled and may result in errors when turning them back on.

Maven skip unit tests with surefire plugin

To skip running the tests for a particular project, set the skipTests property to true.

<project>
    [...]
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${surefire.version}</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
    [...]
</project>

This will allow you to run with tests disabled by default and to run them with this command:

mvn install -DskipTests=false

References

You may also like...