How to skip Maven unit test

In Maven, to skip running unit tests, uses this -Dmaven.test.skip=true

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.

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

1.2 Or defined in pom.xml and run the package command normally.

pom.xml

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

$ mvn package
#no test

2. skipTests

2.1 Alternatively, use this surefire -DskipTests

Terminal

$ mvn package -DskipTests

2.2 Or this.

pom.xml

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

3. maven.test.skip vs skipTests

The core difference :

  1. maven.test.skip is a feature of Maven itself, skip compile tests, skip run test, just ignore any test processes.
  2. skipTests is a feature of surefire plugin, compile the tests but skip running it.

Normally, I prefer maven.test.skip

References

  1. Skipping Tests

About the Author

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

Comments

Leave a Reply

avatar
newest oldest most voted
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.

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.

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

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,

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. […]