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
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.
Uses ‘maven.test.skip’ , it will not compile any unit tests.
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.
Thanks for your info,
Thanks mkyong. I found your tutorial very helpful. Have a great 2021!
Is there a way to do
-DskipTests
with profiles? I want to build tests but not run them.How to skip junit test if programmatically running spring boot application as follows:
String args[] = {};
SpringApplication app = new SpringApplication(SpringBootApplicationClassname.class);
ConfigurableApplicationContext context = app.run(args);
Is there a way to skip a specific package when we try to run the command: mvn clean install
Assume that I have multiple packages with different test cases and for some reason I want to run all packages except from 1 package. Can this be done?
Thank you
Thanks 🙂
working in 2020 xd
Hello, what does -D mean? anyone knows?
Try mvn -?
-D,–define Define a system property
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
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”.
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
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.
Hi mkyong
Your posts are really helpful! Thanks!
Hi Mkyong
Your posts are great! really helpful!
Hello MK, I think it’s better solution for skip test use the properties
3.0.5.RELEASE
……
true
Thanks,
Aleksandar
Sorry, ‘pre’ does not work so good :), here is config
3.0.5.RELEASE
…..
true
Waw! thx A lot master! your tip make my day more easy.
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.
Sorry, did not use the source code comment:
true
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.
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
haha, you need to find out why the tests are failed, skip is just ignore it temporary 🙂
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