Maven – How to skip unit test

In Maven, you can define a system property -Dmaven.test.skip=true to skip the entire unit test.

By default, when building project, Maven will run the entire unit tests automatically. If any unit tests is failed, it will force Maven to abort the building process. In real life, you may STILL need to build your project even some of the cases are failed.

In this article, we will show you a few ways to skip the unit test.

1. maven.test.skip=true

1.1 To skip unit tests, uses this argument -Dmaven.test.skip=true

Terminal

$ mvn package -Dmaven.test.skip=true
#no test

1.2 Or defined in pom.xml

pom.xml

    <properties>
        <maven.test.skip>true</maven.test.skip>
    </properties>
Terminal

$ mvn package
#no test

2. Maven Surefire Plugin

2.1 Alternatively, use this -DskipTests in surefire plugin.

Terminal

$ mvn package -DskipTests
#no test

2.2 Or this.

pom.xml

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

2.3 To skip some test classes.

pom.xml

	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-surefire-plugin</artifactId>
		<version>3.0.0-M1</version>
		<configuration>
			<excludes>
				<exclude>**/TestMagic*.java</exclude>
				<exclude>**/TestMessage*.java</exclude>
			</excludes>
		</configuration>
    </plugin>

3. Maven Profile

3.1 Create a custom profile to skip the unit test.

pom.xml

	<profiles>
        <profile>
			<id>xtest</id>
			<properties>
				<maven.test.skip>true</maven.test.skip>
			</properties>
		</profile>
    </profiles>

3.2 Active the profile with a -P option.

Terminal

$ mvn package -Pxtest
#no test

References

  1. Maven Surefire Plugin
  2. Inclusions and Exclusions of Tests
  3. Skipping Tests
  4. Maven Profiles example
  5. How to run unit test with Maven

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
16 Comment threads
7 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
15 Comment authors
BilalMurphySmithf77Davemkyong Recent comment authors
newest oldest most voted
Monica
Guest
Monica

Wrong. The “unit tests will be ignored” is just wrong. The Maven guys whine that it is too hard to get Maven to ignore and not compile unit tests so the -DskipTests only doesn’t execute the unit tests. It doesn’t ignore them and not compile.

It’s amazing how childish Maven people are. They could have fixed their problem in less time than it takes them to whine about how it is difficult to implement. Grow up.

mkyong
Guest
mkyong

Uses ‘maven.test.skip’ , it will not compile any unit tests.

Laurent C.
Guest
Laurent C.

I can argue about this commandline but this blog made it better:

Source: Learning by Experience

Recently came across this the existance of the -DskipTests argument while running maven.

From the userguide:

You can also skip the tests via command line by executing the following command:

mvn install -DskipTests

If you absolutely must, you can also use the maven.test.skip property to skip compiling the tests. maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin.

mvn install -Dmaven.test.skip=true

Skiptests is a feature of surefire, while -Dmaven.test.skip is a feature of maven itself.

mkyong
Guest
mkyong

Thanks for your info,

Murphy
Guest
Murphy

Hello, what does -D mean? anyone knows?

Bilal
Guest
Bilal

I think it stands for “define”, use as -D or –define to set system properties these can declared in the profile or in properties as shown above. see here for more info http://maven.apache.org/ref/3.1.0/maven-embedder/cli.html

Dave
Guest
Dave

Thanks! I am building some open source project that expects tests to be run in the context of a container so they fail on my machine, and in fact I am not editing the code just building the package so there is no need for me to test the code when it is already build successful tests passing in github. Thanks for the surefire method which works for me, the maven way didn’t work for me, I got this error: Unknown lifecycle phase “.test.skip=true”.

NIKHILESH CHAURASIA
Guest
NIKHILESH CHAURASIA

Hey MK,

Can you please guide me if there is any way to skip only single TEST class out of 20 test classes while “mvn clean package”. Because in my case only once test case is failing.

Thanks

randy.su
Guest
randy.su

More request, how to skip all steps except deploy when run “mvn tomcat:depoy”? (as I already run “mvn:install”). Actually I want to know how to skip some steps which have been done before.

Sean
Guest
Sean

Hi mkyong

Your posts are really helpful! Thanks!

Sean
Guest
Sean

Hi Mkyong

Your posts are great! really helpful!

Aleksandar Bosancic
Guest
Aleksandar Bosancic

Hello MK, I think it’s better solution for skip test use the properties

3.0.5.RELEASE
……
true

Thanks,
Aleksandar

Aleksandar Bosancic
Guest
Aleksandar Bosancic

Sorry, ‘pre’ does not work so good :), here is config

3.0.5.RELEASE
…..
true

alka
Guest
alka

Waw! thx A lot master! your tip make my day more easy.

Scott
Guest
Scott

Mykong,

using true in the pom causes Maven to skip test execution. Is there any way to tell Maven to not compile the tests as well using a element in the pom instead of command line? Something such as true

Thanks.

Scott
Guest
Scott

Sorry, did not use the source code comment:

true

Pete
Guest
Pete

Well, the test was one we could safely ignore:
testFindDomainBounds in the TimeSeriesCollectionTests returned:
expected: but was:
So we can assume it’s a basic rounding error, but very minor so safe to ignore.
Not sure if it’s a Mac thing, perhaps a 64-bit issue but the test should be updated to round somewhat.

Pete
Guest
Pete

Thanks alot mkyong. I had one unit test out of over 2000 fail in jFreeChart! Annoying, but your tip saved the day. Thanks again…
Pete
Melbourne, Australia

trackback
How to skip Maven unit test | Just another software blog

[…] Mkyong] By default, when you build your project with maven, it will run the entire unit tests of your […]

trackback
Maven Tutorials | Tutorials

[…] Skip unit test during Maven build Here’s a tip to skip Maven to run your project unit test during project build, because run all the project unit test by default is not necessary in all times. […]

Smithf77
Guest
Smithf77

Right now it looks like WordPress is the top blogging platform available right now. from what I’ve read Is that what you’re using on your blog? bkcceddgfdddcebe