Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Medium

Introduction to Maven Skip Test

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Welcome back, Ninjas; we are going to learn different ways to skip tests in Maven.

Maven Skip test

Testing is important while building the applications written in Maven structure work with multiple test cases or not. Skipping tests is a bad idea; however, sometimes it is necessary to skip them to save time or the overheads, especially for a large project.

In these cases, we explore all the possible commands to skip the tests in Maven.

Maven

Before moving into much more detail, let's start.

Ways to Skip Tests in Maven

There are various methods using which we can skip tests in Maven.

  • The first way is to do it by using the command line.
  • By using maven.test.skip property while using the maven command.
  • Using skipping by default and then overriding it when necessary.
  • By excluding some of the test cases using the exclude command.

Skipping the Tests

If you want to skip tests in Maven for a project and are using the maven-surefire-plugin in your pom.xml, then it's pretty easy to do so. You can specify the skipTests property = true in your plugin tag.

Before applying the properties:

Running the tests cases

As you can see that our test cases are running properly. Till now we have not applied any properties in the plugin tag.

After specifying the properties where we will write the command to skip the tests.

Code

<project>
  <properties>
        <maven.test.skip>true</maven.test.skip>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.Maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <plugin>
        </plugins>
    </build>
  <dependencies>
  <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.6.1</version>
    <scope>test</scope>
</dependency>
  </dependencies>

</project>


Output:

Tests are Skipped

Explanation:

Now you can see that Tests are skipped as the data under the TestCase1 file is not printed. 

Using Command Line

You can skip tests in Maven using the command line by executing the below command:

mvn install -DskipTests


Code:

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>3.0.0-M7</version>
				<configuration>        
					<skipTests>true</skipTests>      
				</configuration>
			</plugin>
		</plugins>
	</build>
  
	<dependencies>
  		<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
  		<dependency>
    		<groupId>org.seleniumhq.selenium</groupId>
    		<artifactId>selenium-java</artifactId>
    		<version>4.6.0</version>
  		</dependency>
  		<!-- https://mvnrepository.com/artifact/org.testng/testng -->
  		<dependency>
    		<groupId>org.testng</groupId>
    		<artifactId>testng</artifactId>
    		<version>7.6.1</version>
    		<scope>test</scope>
  		</dependency>
  	</dependencies>
</project>


Output:

Here the command -DskipTests test command is used to run the program and skip the tests.

-DskipTests test command is used

Explanation:

As you can see there is no test execution and Tests are Skipped. So using -DskipTests text command under the configuration is executed to skip the tests.

Output 2:

Here the command install -DskipTests = true is used to skip the tests.

install -DskipTests = true is used to skip the tests.

Explanation:

Using this command also we can skip the tests, Code written above will remain same for both the commands.

Using Maven.test.skip property

If you want to skip tests in Maven, you can use Dmaven.test.skip=true command in Maven. It will not compile and execute the test cases. This property is supported by most of the Maven testing plugins, including failsafe, surefire, and even the compiler plugin of Maven.

mvn package -Dmaven.test.skip=true


Output:

Here, the command package -Dmaven.test.skip= true is used to skip the tests.

package -Dmaven.test.skip= true is used to skip the tests.

Explanation:

As you can see from the output, there is no test execution by using the command package -Dmaven.test.skip= true.

Note: The default value of the -DskipTests argument is considered true when used without assigning the value.

Excluding some of the test classes

We can skip certain classes using the excluded element of the pom file by specifying them.

Code

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M7</version>
        <configuration>
        <excludes>
       		<exclude>**/TestCase1*.java</exclude>
        </excludes>
       </configuration>
     </plugin>
    </plugins>
</build>


Output:

package -DskipTests is used to skip the tests

Explanation:

As you can see Test file will get excluded because it is mentioned in exclude, i.e., the element of configuration. The command which we will be using to exclude the test cases will be package -DskipTests.

Skipping by default

If you want to skip tests in Maven by default and want the ability to execute them sometimes when there is a necessity. Whenever we find the necessity to perform the test on our project, we may override the value of this property(Property value as true) by specifying the property value in the command that will be executed for Maven on the command line.

Code

<project>
  [...]
  
    <properties>
        <skipTests>true</skipTests>
    </properties>
  [...]
  
   <build>
        <plugins>
            <plugin>
                <groupId>org.apache.Maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <skipTests>${skipTests}</skipTests>
                </configuration>
             </plugin>
         </plugins>
    </build>
  [...]

</project>


Now, let's suppose, in the middle of the application, we feel a necessity to test the ongoing project; then, we can do that by setting the value of the property-> defaultValueOfSkip to false in our command as follows

Command:

mvn package -DdefaultValueOfSkip=false


OR

mvn install -DdefaultValueOfSkip=false


Explanation:

The command package -DdefaultValueOfSkip=false will revert our action of skipping the test cases. In simpler words if you feel that you want to execute the test cases then you can set the value of the defaultValueOfSkip to false.

Also read, Nmap commands

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What is Maven?

Maven is a one-stop shop for automating tools and project management services for various programming languages, primarily Java and C#.

Name some of the Maven Commands.

Maven commands are: mvn install, mvn package, mvn clean, mvn validate, etc.

What are the different ways to skip the test in Maven?

Different ways to skip tests in Maven are Using Command Line, Skipping the default, and Excluding some test classes.

Is it a good idea to skip the tests?

Skipping the tests is a bad idea; however, sometimes it is necessary to skip them to avoid overhead on the system when we compile and run the tests.

How do you skip the test in Maven?

If you want to skip tests in Maven for a particular project, then you can specify the skipTests property = true in your plugin tag. By executing the below command:

mvn install -DskipTests.

Conclusion

In this article, we have explored all the different ways to skip tests in Maven. It helps us to skip compiling and/or running the tests. We went through skipping the tests, excluding the test classes, and skipping by default. However, deploying the project without testing is bad practice, so ensure there is some specific purpose for skipping the tests.

Let's continue here and read out more stuff related to Maven.

  1. Maven Configuration Properties
  2. Maven Interview Questions
  3. Maven Architecture
     

Check out The Interview guide for Product Based Companies and some of the Popular Interview Problems from Top companies like AmazonAdobeGoogleUberMicrosoft, etc., on Coding Ninjas Studio.

Also, check out some of the Guided Paths on topics such as Data Structures and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test SeriesInterview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Cheers!

Topics covered
1.
Introduction
2.
Ways to Skip Tests in Maven
2.1.
Skipping the Tests
2.1.1.
Using Command Line
2.1.2.
Using Maven.test.skip property
2.2.
Excluding some of the test classes
2.3.
Skipping by default
3.
Frequently Asked Questions
3.1.
What is Maven?
3.2.
Name some of the Maven Commands.
3.3.
What are the different ways to skip the test in Maven?
3.4.
Is it a good idea to skip the tests?
3.5.
How do you skip the test in Maven?
4.
Conclusion