EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Maven Tutorial Maven Skip Test
Secondary Sidebar
Maven Tutorial
  • Maven Basic and advance
    • What is Maven
    • How to Install Maven
    • Maven Commands
    • Maven GroupId
    • Maven WAR Plugin
    • Maven Build Command
    • Maven Failsafe Plugin
    • Maven Profile
    • IntelliJ Maven
    • Maven Enforcer Plugin
    • Maven Javadoc Plugin
    • Maven WAR Plugin
    • Maven Build Command
    • Maven GroupId
    • Maven Force Update
    • Maven Encrypt Password
    • Maven Environment Variables
    • AssertJ Maven
    • Maven Run Single Test
    • Maven kafka-clients
    • Maven Quickstart Archetype
    • Maven Install Dependencies
    • Maven XMLBeans
    • Maven Local Repository
    • Maven Versions
    • Maven Jar Plugin
    • Maven Assembly Plugin
    • Maven exec plugin
    • Maven Central Repository
    • Maven Surefire
    • Maven Deploy
    • Maven Phases
    • Maven Archetype
    • Maven Skip Test
    • Maven Dependency Scope
    • Maven Shade Plugin
    • Maven Repository Spring
    • Maven Eclipse Plugin
    • Maven Exclude Dependency
    • Maven Life Cycle
    • Maven Repository
    • Maven POM File
    • Maven Plugins
    • What is Maven Plugins
    • Maven Interview Questions
    • Maven Flags
    • Maven Project
    • Maven Settings.XML

Maven Skip Test

By Payal UdhaniPayal Udhani

Maven Skip Test

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,818 ratings)

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:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>

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:

Command:

mvn package -Dmaven.test.skip=true

Note: It is necessary to specify the value of this argument to true to skip the test compilation.

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.

Code:

<project>
<properties>
<defaultValueOfSkip>true</defaultValueOfSkip>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<skipTests>${defaultValueOfSkip}</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>

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:

Command:

mvn package -DdefaultValueOfSkip=false

OR

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.

Code:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<excludes>
<exclude>SampleTest.java</exclude>
</excludes>
</configuration>
</plugin>

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.

Conclusion

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.

Recommended Articles

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 –

  1. Java Deployment Tools
  2. Maven Commands
  3. Maven POM File
  4. Maven Repository
Popular Course in this category
Maven Training (4 Courses, 6 Projects)
  4 Online Courses |  6 Hands-on Project |  26+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more