In this short tutorial, we are going to shed light on how to skip tests with Maven.

In short, skipping tests is considered a bad practice. However, we may come across a situation where we need to skip compiling and running unit tests, especially when we are developing a new code or if the tests are outdated.

So, let’s explore all the most commonly used methods to ignore tests with Maven.

Note: If you have Mac and you did not install Maven yet, please refer to how to install maven on mac.

Using maven.test.skip Property

Before diving deep into how to make maven skip tests, let’s take a look at an example of a unit test that doesn’t compile:

    
        @Test
        public void myUnitTest() {
            devwithus.com;
        }
    

Now, let’s try to run the mvn package command:

    
        C:\Users\devwithus\projects\students> mvn package
        ...
        [INFO] -------------------------------------------------------------
        [ERROR] COMPILATION ERROR :
        [INFO] -------------------------------------------------------------
        [ERROR] /C:/Users/devwithus/projects/students/src/test/java/com/rest/students/tests/ApplicationTests.java:[11,22] not a statement
        [INFO] 1 error
        [INFO] -------------------------------------------------------------
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD FAILURE
        [INFO] ------------------------------------------------------------------------
        [INFO] Total time:  3.542 s
        [INFO] Finished at: 2021-06-14T22:22:34+02:00
        [INFO] ------------------------------------------------------------------------
        [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on project students: Compilation failure
        [ERROR] /C:/Users/devwithus/projects/students/src/test/java/com/rest/students/tests/ApplicationTests.java:[11,22] not a statement
        [ERROR]
        [ERROR] -> [Help 1]
        [ERROR]
        [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
        [ERROR] Re-run Maven using the -X switch to enable full debug logging.
        [ERROR]
        [ERROR] For more information about the errors and possible solutions, please read the following articles:
        [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
    

As we can see, the build fails. So, to avoid the compilation error, we need to skip compiling our unit test.

To do so, we can run our command with the -Dmaven.test.skip=true option:

    
        C:\Users\devwithus\projects\students>mvn package -Dmaven.test.skip=true
        [INFO] Scanning for projects...
        [INFO]
        [INFO] -------------------------< com.rest:students >--------------------------
        [INFO] Building students 0.0.1-SNAPSHOT
        [INFO] --------------------------------[ jar ]---------------------------------
        [INFO]
        [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ students ---
        [INFO] Using 'UTF-8' encoding to copy filtered resources.
        [INFO] Copying 1 resource
        [INFO] Copying 1 resource
        [INFO]
        [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ students ---
        [INFO] Nothing to compile - all classes are up to date
        [INFO]
        [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ students ---
        [INFO] Not copying test resources
        [INFO]
        [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ students ---
        [INFO] Not compiling test sources
        [INFO]
        [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ students ---
        Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.22.2/maven-surefire-common-2.22.2.pom
        Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.22.2/maven-surefire-common-2.22.2.pom (11 kB at 1.2 kB/s)
        ...
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------
        [INFO] Total time:  02:06 min
        [INFO] Finished at: 2021-06-14T22:29:09+02:00
        [INFO] ------------------------------------------------------------------------
    

As shown above, Maven skips the tests. The log clearly states that there is Nothing to compile - all classes are up to date.

Alternatively, we can avoid compiling unit tests by configuring the maven.test.skip property in pom.xml:

    
        <properties>
            <maven.test.skip>true</maven.test.skip>
        </properties>
    

That way, skipping the tests become the default behavior. However, we can always override it by using maven.test.skip=false in the command line:

    
        mvn package -Dmaven.test.skip=false
    

Please bear in mind that maven.test.skip property is honored by the most popular plugins such as Surefire, Failsafe, and the Compiler Plugin.

Skip Tests in Maven using DskipTests

DskipTests offers another way to ignore tests. The main difference between these two properties is that DskipTests compiles the unit tests, but skips executing them.

Now that we know that DskipTests skips the tests execution, let’s take a look at a unit test that does fail:

    
        @Test
        public void myTestThatFails() {
            fail("I can't be executed, I'm sorry");
        }
    

Let’s execute the mvn package command and see the output:

    
        ...
        [ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.347 s <<< FAILURE! - in com.rest.students.tests.ApplicationTests
        [ERROR] myTestThatFails  Time elapsed: 0.001 s  <<< FAILURE!
        org.opentest4j.AssertionFailedError: I can't be executed, I'm sorry
                at com.rest.students.tests.ApplicationTests.myTestThatFails(ApplicationTests.java:14)
        [INFO] Results:
        [INFO]
        [ERROR] Failures: 
        [ERROR]   ApplicationTests.myTestThatFails:14 I can't be executed, I'm sorry
        [INFO]
        [ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0
        [INFO]
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD FAILURE
        [INFO] ------------------------------------------------------------------------
        [INFO] Total time:  9.920 s
        [INFO] Finished at: 2021-06-15T17:57:58+02:00
        [INFO] ------------------------------------------------------------------------
        [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project students: There are test failures.
        ...
    

As we can see the build fails because our unit test is not executed with success. So, to fix that, we can use the DskipTests property to ignore tests execution:

    
        C:\Users\Asus\TP\students>mvn package -DskipTests
        ...
        [INFO] BUILD SUCCESS
        [INFO] ------------------------------------------------------------------------
        [INFO] Total time:  2.525 s
        [INFO] Finished at: 2021-06-15T18:11:41+02:00
        [INFO] ------------------------------------------------------------------------
    

Maven Surefire Plugin

Alternatively, we can configure the skipTests property with Surefire plugin to make Maven skip unit tests:

    
        <properties>
            <maven.skip.tests>true</tests.skip>
        </properties>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>${maven.skip.tests}</skipTests>
            </configuration>
        </plugin>
    

Therefore, we can override this behavior with the help of the following command line:

    
        mvn package -Dmaven.skip.tests=false 
    

Maven Skip Specific Tests

We can also configue Surefire plugin to avoid the execution of some particular test classes only:

    
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>**/MyUnitTest*.java</exclude>
                </excludes>
            </configuration>
        </plugin>
    

Simaraly, we can use the -Dtest option with the exclamation mark (!) to tell Maven to exclude specific tests:

    
        # Exclude one test class
        mvn test -Dtest=!MyUnitTest
        # Exclude one test method
        mvn test -Dtest=!MyUnitTest#myTestCase
    

Starting from Surefire 2.19, we can select multiple methods:

    
        mvn test -Dtest=!MyUnitTest#myTestOne+myTestTwo
    

Skipping Tests on Specific Profile

Sometimes, we need to skip tests on a specific profile, for example on the development environment only.

Let’s see how we can tell Maven to run tests on all profiles except the dev profile:

    
        <profiles>
            <profile>
              <id>dev</id>
              <properties>
                <maven.test.skip>true</maven.test.skip>
              </properties>
            </profile>
         </profiles>
    

We can specify a profile using the -P option:

    
        mvn package -Pdev
    

That way, Maven will skip tests on the specified environment, dev in our case.

Conclusion

To sum it up, we have explained in detail how to make Maven skip tests using maven.test.skip and DskipTests properties.

Along the way, we have seen how to use Maven Surefire Plugin to achieve the same purpose.

Finally, we have discussed how to exclude particular unit tests and skip running them in a specific environment.