Introduction to Maven Skip Test
Maven build lifecycle provides us to perform various actions on our project that are necessary such as verify to make sure everything is specified correctly in the project, further the project is cleaned, packaged, tested, and deployed. Testing is an important part of the build process that makes sure that the application or our project is written in the maven structure works correctly for the multiple test cases and should not be skipped as it is considered bad practice to deploy the project without testing. However, there are some situations in which we need to skip the test operation. For example, while working on the new module and we want to run the builds that are not passing or compiling to check them on an intermediate basis. In these cases, we can skip the testing to avoid overhead on the system that occurs when we compile and run the tests. In this article, we will learn about various methodologies that can be used to skip the test in the maven project.
How Testing works in Maven
Let us know how testing works in maven. The Maven build lifecycle can ignore, compile, or run the tests. There are various plugins that can be used for testing such as failsafe plugin, surefire plugin, and many others. Out of them skipping the testing is supported by compiler plugin, failsafe plugin, and surefire plugin. In this article, we will execute the package phase that compiles and runs the test and will use the surefire plugin in our examples.
Various ways to Skip the Test in Maven
There are various ways using which we can skip the testing in maven.
- skip tests element property can be set inside the configurations of your plugin tag.
- Through command line using -DskipTests argument in your maven execution command.
- By using the maven.test.skip property while firing the maven command for executing the phase.
- Skipping by default by initializing the skipTests element value to true in properties and then overriding it through the command line by mentioning the value of -DskipTests in the maven phase/build execution command.
- Excluding some of the test classes by using the exclude element with the name of the class to be excluded in your pom.xml’s plugin tag while using surefire.
Let us study all the above methods one by one:
1. skip tests element in pom
If you wish to skip the test for a certain project, you can specify the skipTests property to true in your plugin tag of the project’s pom.xml file in the following way:
that is placed inside the plugins element of build tag in your project element of the pom.
2. DskipTests argument
You can skip the test for any project while executing the maven command from command prompt for that particular project’s build lifecycle’s phase by specifying the argument -DskipTests. The default value of -DskipTests argument is considered as true when used without assigning the value. Suppose, I am executing the package command for the current maven project by using mvn package then instead of the above command, I will fire the following command to be executed in order to skip the test of that maven project.
3. maven.test.skip property
If you have to completely skip the test compilation then you can even make the use of the maven-test-skip property in your command of maven. This property is supported by most of the testing plugins including failsafe and surefire and even the compiler plugin of maven. The above property can be used in following ways while executing the maven command from the command line:
mvn package -Dmaven.test.skip=true
4. Skipping by default and then overriding it when necessary
There is often a need while developing the application to skip the test by default and execute it sometimes when there is a necessity. As the development involves too many changes and running the command for every change will lead to large overhead of compiling and running the test if the test is not skipped in maven projects.
In these situations, we can mention the default behavior of the maven project to skip the test for each maven command fired. This is specified inside the pom.xml file by initializing a property variable to true and assigning its value to the skip Tests element. Whenever there is a necessity to perform the test, we can override the value of this property by specifying the property value in our command to be executed for maven on the command line. This can be done in the following way –
Firstly, pom.xml needs to include the property that is set to true, in our case defaultValueOfSkip is the property whose value is set to true and then this property is assigned to the skip Tests element of the configuration of surefire plugin element. Hence, whenever We fire the mvn package or any other maven phase command such as mvn install the test will be skipped by default.
Now, suppose, in the middle of application development, I feel a necessity to test the project, then I can do so, by simply setting the value of the property defaultValueOfSkip to false in my command in the following way:
mvn package -DdefaultValueOfSkip=false
mvn install -DdefaultValueOfSkip=false
and likewise for all other phases occurring after a test phase in a sequence of order of phases.
5. Excluding some of the test classes
While using the surefire plugin, we can skip the test for certain classes by specifying them inside the excluded element of the pom file.
This will lead to the exclusion of the SampleTest file for unit testing even when it contains the test word in its name because it was mentioned in exclude the element of configuration element in the pom.xml file.
We can skip the test in maven projects in various ways by using command-line commands or pom.xml file by using the above-mentioned ways. However, we should make sure that the test is skipped only when some specific purpose is there as it is a bad practice to deploy the project without testing.
This is a guide to Maven Skip Test. Here we discuss an introduction to Maven Skip Test, how does it work, various ways to skip the test with explanation. You can also go through our other related articles to learn more –