In this tutorial, we will see how to skip tests in maven.
It is not a good idea to skip junit tests while building project in maven but sometimes, you may need to skip it for building new code and want to test it quickly.
For example: In a large project, where you have more than 1000 junit test cases
and you want to change small piece of code and run it quickly to test it. You may want to skip running test cases to save time and should consider this as an intermediate build.
Using maven.test.skip=true
You can use -Dmaven.test.skip=true
to skip test cases. It will not compile and execute
the test cases.
Here is an example:
Go to the project location on terminal and execute below command.
You can do the same in pom.xml with the following properties.
1 2 3 4 5 6 7 |
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.jupiter.version>5.5.1</junit.jupiter.version> <maven.test.skip>true</maven.test.skip> </properties> |
Here is an example of complete pom.xml
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.arpit.java2blog</groupId> <artifactId>JunitExamples</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>JunitExamples</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.jupiter.version>5.5.1</junit.jupiter.version> <maven.test.skip>true</maven.test.skip> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project> |
Execute below command to skip test via properties taf.
You can use this property in maven profile as well.
1 2 3 4 5 6 7 8 9 10 |
<profiles> <profile> <id>skipTestMavenProfile</id> <properties> <maven.test.skip>true</maven.test.skip> </properties> </profile> </profiles> |
Execute below command to skip test via profile.
Using SkipTests
You can use -DskipTest also to skip tests. This will still compile the testcases but will not execute them.
In pom.xml, you can use it with Maven Surefire Plugin.
1 2 3 4 5 6 7 8 9 10 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> |
Skip tests in Maven in eclipse
You can use above options to skip tests in eclipse. Eclipse also provide checkbox to skip test cases in eclipse.
Here is the screenshot for the same.
Conclusion
You can use -Dmaven.test.skip=true
or -DskipTests
to skip tests in maven. Please note that
-Dmaven.test.skip=true
will neither compile the testcases nor execute them while -DskipTests
will compile the testcases but will not execute them.
That’s all about how to skip tests in maven.