In this quick guide, we discuss several use cases on how to skip unit test cases using maven mvn command when you are working with clean install or package a build.
Skipping tests is bad idea, but there might be situations where we need to skip tests in some environments (for example local) or we might need to build application without compiling and running tests, as tests are outdated. Let’s see how we can skip tests with Maven.
1. maven.test.skip=true
Define maven.test.skip system property and value should be true in pom.xml
or from command line, Maven will skip the tests. In this tutorial, to run tests, we used install and package phases and configured Maven Surefire Plugin. Let’s see an example.
1.1. From Command line
Skip tests in install or package phase.
$ mvn clean install -Dmaven.test.skip=true OR $ mvn clean package -Dmaven.test.skip=true
Results:
3 Phases, 1. Copying test resources, 2. Compiling test sources and 3. Tests are skipped
1.2. From pom.xml configuration
If you would like to skip tests by default, you need to define maven.test.skip in pom.xml
and set value true. This value can be changed to false using command line flag -Dmaven.test.skip=false to not to skip tests.
In pom.xml:
<properties> <maven.test.skip>true</maven.test.skip> </properties>
The above configuration skips all the tests by default when you build application using maven. To not skip tests, you need to set -Dmaven.test.skip value as false
.
$ mvn clean install -Dmaven.test.skip=true
2. skipTests=true
In some cases, you may require compile test sources and only just need to skip tests. Similarly maven.test.skip, you can define system property skipTests from command line or in pom.xml
.
From Command:
$ mvn clean install -DskipTests OR $ mvn clean package -DskipTests
Results: Only skips running tests, and Copying test resources, Compiling test sources phase will not be skipped.
In pom.xml:
<properties> <skipTests>true</skipTests> </properties>
To not skip tests, use command $ mvn clean package -DskipTests=false
.
3. Maven surefire plugin
Alternatively skipTests property can define in surefire plugin. By default all tests will be skipped.
<properties> <skipTests>true</skipTests> </properties> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${maven-surefire-plugin.version}</version> <configuration> <skipTests>${skip.tests}</skipTests> </configuration> </plugin>
To not skip tests, you have to use following command.
$ mvn clean package -Dskip.tests=false
4. Skip Tests on specific environment
For example, you would need to skip tests on Local environment and should continue on other environments like Prod or QA, we can use Maven Profiles.
<profiles> <profile> <id>local</id> <properties> <maven.test.skip>true</maven.test.skip> </properties> </profile> </profiles>
To run profile, following command can be used.
$ mvn clean package -Plocal
5. Conclusion
In this tutorial, we covered how to skip unit test cases using maven mvn command when you are working with clean install or package a build.
You also might interested in following Maven guides:
- Install Maven on Windows
- Install Maven on Mac
- Install Maven on Ubuntu
- Import maven project into Eclipse
- Maven run single test
- Maven set java version
- Maven run java main class