Skipping Tests With Maven

In ideal scenario, we want to compile and run all our tests before building any project. But in rare scenarios we might want to skip our tests, for example – when we’re developing new code and want to run intermediate builds in which the tests are not passing or compiling.

In this post, we’ll see all possible commands and options to skip tests using Maven.

Using Command Line Flags:

– Skipping the Test Compilation:

Lets look at a test which will never compile –

import org.junit.Assert;

public class FirstTest {

    public static void main(String[] args){

        Assert.assertTrue(j>1);

    }
}

When we run the command: mvn package

We will get the below error:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.372 s
[INFO] Finished at: 2020-03-29T11:30:08+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on project sample: Compilation failure -> [Help 1]

In Maven, we can skip the compilation phase for test sources by using the below command:

mvn -Dmaven.test.skip package

Build will be successful after running the above command:

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sample ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ sample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ sample ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ sample ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ sample ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ sample ---
[INFO] Building jar: C:\Users\Bijan\IdeaProjects\sample\target\sample-1.0-Snapshot.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.619 s
[INFO] Finished at: 2020-03-29T11:36:28+05:30
[INFO] ------------------------------------------------------------------------

– Skipping the Test Execution

Let’s change our earlier test so that it always fails

import org.junit.Assert;

public class FirstTest {

    public static void main(String[] args){

        int j=1;
        Assert.assertTrue(j>1);

    }
}

Run the maven command: mvn package

We will get the below error:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.261 s
[INFO] Finished at: 2020-03-29T11:41:22+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on project sample: Compilation failure -> [Help 1]

In Maven we can skip running the tests by executing the below command:

mvn -DskipTests package

Build will be successful after running the above command:

C:\Users\Bijan\IdeaProjects\sample>mvn -DskipTests package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< org.qascript:sample >-------------------------
[INFO] Building sample 1.0-Snapshot
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sample ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ sample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ sample ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\Bijan\IdeaProjects\sample\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ sample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ sample ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ sample ---
[INFO] Building jar: C:\Users\Bijan\IdeaProjects\sample\target\sample-1.0-Snapshot.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.208 s
[INFO] Finished at: 2020-03-29T14:20:32+05:30
[INFO] ------------------------------------------------------------------------

Using Maven Plugin Configuration:

We can skip the tests by adding the following configuration within the Surefire plugin:

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M4</version>
                    <configuration>
                        <includes>
                            <include>FirstTest.java</include>
                        </includes>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

We can run the tests but ignore the test failures by adding the following configuration to the Surefire plugin:

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M4</version>
                    <configuration>
                        <includes>
                            <include>FirstTest.java</include>
                        </includes>
                        <testFailureIgnore>true</testFailureIgnore>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

 

 

 

Share

Bijan Patel

Founder & Creator of QAScript | 12+ years of IT Experience | Full Stack Automation Engineer | Blogger | Trainer

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!