Home Maven Maven - How to skip tests?

Maven – How to skip tests?

In this quick guide, we discuss how to skip unit test cases using maven 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 a system property maven.test.skip and value should be true in pom.xml or from command line. Maven will skip the tests. In this tutorial to run tests, 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, 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>

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 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 use, 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 command when you are working with clean install or package a build.

You also might interested in following Maven guides:

  1. Install Maven on Windows
  2. Install Maven on Mac
  3. Install Maven on Ubuntu
  4. Import maven project into Eclipse
  5. Maven run single test
  6. Maven set java version
  7. Maven run java main class

6. References

  1. Surefire Plugin
  2. skip tests

LEAVE A REPLY

Please enter your comment!
Please enter your name here