cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Prevent unit tests but allow integration tests in Maven

I've a Maven build in which I use the SureFire plugin to run some unit tests, and the FailSafe plugin to run some integration tests. I would like a way to run just the FailSafe plugin's tests.

It's not a good solution for me to add different profiles or anything in the pom, because it's a multimodule build and I don't want to have to edit every module's pom.

There are skip.tests and maven.test.skip and skipTests which stop all tests, and skipITs, which stops only the failsafe plugin.

So, is there a command-line flag for Maven like skipITs, but instead with the functionality of "onlyITs"?

2 REPLIES 2

Skipping Tests
To skip running the tests for a particular project, set the skipITs property to true.

<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<skipITs>true</skipITs>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
You can also skip the tests via the command line by executing the following command:

mvn install -DskipTests
Since skipTests is also supported by the Surefire Plugin, this will have the effect of not running any tests. If, instead, you want to skip only the integration tests being run by the Failsafe Plugin, you would use the skipITs property instead:

mvn install -DskipITs
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
Skipping by Default
If you want to skip tests by default but want the ability to re-enable tests from the command line, you need to go via a properties section in the pom:

<project>
[...]
<properties>
<skipTests>true</skipTests>
</properties>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<skipITs>${skipTests}</skipITs>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
This will allow you to run with all tests disabled by default and to run them with this command:

mvn install -DskipTests=false
The same can be done with the skip parameter and other boolean properties of the plugin. 

milliron52401
Member

I found the simplest way to skip only surefire tests is to configure surefire (but not failsafe) as follows:

 

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.14</version>
    <configuration>
        <!-- skips surefire tests without skipping failsafe tests.
                 Property value seems to magically default to false -->
        <skipTests>${skip.surefire.tests}</skipTests>
    </configuration>
</plugin>

This allows you to run mvn verify -Dskip.surefire.tests and only surefire, not failsafe, tests will be skipped; it will also run all other necessary phases including pre-integration and post-integration, and will also run the verify goal which is required to actually fail your maven build if your integration tests fail.

Note that this redefines the property used to specify that tests should be skipped, so if you supply the canonical -DskipTests=true, surefire will ignore it but failsafe will respect it, which may be unexpected, especially if you have existing builds/users specifying that flag already. A simple workaround seems to be to default skip.surefire.tests to the value of skipTests in your <properties> section of the pom:

<properties>
    <skip.surefire.tests>${skipTests}</skip.surefire.tests>
</properties>

If you need to, you could provide an analagous parameter called skip.failsafe.tests for failsafe, however I haven't found it necessary - because unit tests usually run in an earlier phase, and if I want to run unit tests but not integration tests, I would run the test phase instead of the verify phase. Your experiences may vary!

These skip.(surefire|failsafe).tests properties should probably be integrated into surefire/failsafe code itself, but I'm not sure how much it would violate the "they're exactly the same plugin except for 1 tiny difference" ethos.

my IDE is complaining about "cannot resolve symbol 'skipTests'" the solution was to add a line <skipTests>false</skipTests> still works with any combination of -DskipTests or -Dskip.surefire.tests as command line args seem to overwrite properties stackoverflow.com/questions/13708738/โ€ฆ  /omeglz /echat you may want to add that to your solution