Prevent unit tests but allow integration tests in Maven

MavenBuildIntegration TestingMaven Surefire-PluginMaven Failsafe-Plugin

Maven Problem Overview


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"?

Maven Solutions


Solution 1 - Maven

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.

Solution 2 - Maven

A workaround would be to call:

mvn clean test-compile failsafe:integration-test

Admittedly, this is ugly, but it may solve your problem.


Or (another hack):

mvn clean integration-test -Dtest=SomePatternThatDoesntMatchAnything -DfailIfNoTests=false

Reference:

Solution 3 - Maven

I am using the code from Antonio Goncalves Blog , which works perfect.

You can use the following properties:

-DskipUTs=true for skipping surefire tests.

-DskipITs=true for skipping failsafe tests.

-DskipTests=true for skipping all tests.

The pom.xml is as follows:

<properties>
    <skipTests>false</skipTests>
    <skipITs>${skipTests}</skipITs>
    <skipUTs>${skipTests}</skipUTs>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <skipTests>${skipUTs}</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19.1</version>
            <executions>
                <execution>
                    <id>run-integration-tests</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <skipTests>${skipTests}</skipTests>
                <skipITs>${skipITs}</skipITs>
            </configuration>
        </plugin>
    </plugins>
</build>

Solution 4 - Maven

Hope this helps!

Try to run test only with FailSafe (plugin for integration-test - it will allow you to run only integration tests with this kind naming, by default: */IT.java, **/*IT.java, **/*ITCase.java; , but you can easily change that from pom file)

mvn failsafe:integration-test

And when you want to use only SureFire (plugin for unit-testing)

mvn surefire:test

or one test at a time with:

mvn -Dtest=MyUnitlTest

Solution 5 - Maven

I do like this so every phases are normally executed:

 mvn -Dtest=foo -DfailIfNoTests=false verify

Solution 6 - Maven

This skips the UnitTests triggered by SureFire and tests only one test:

mvn failsafe:integration-test -Dit.test=YourTestName

You can also provide a pattern: mvn failsafe:integration-test -Dit.test=*

(Maven 3.6.3)

Solution 7 - Maven

Try running your integration or unit tests in a separate profile. Then you can just enable/disable the profile.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthew GilliardView Question on Stackoverflow
Solution 1 - MavenbacarView Answer on Stackoverflow
Solution 2 - MavenSean Patrick FloydView Answer on Stackoverflow
Solution 3 - MavenMartijn BurgerView Answer on Stackoverflow
Solution 4 - MavenKati HolaszView Answer on Stackoverflow
Solution 5 - MavenJérôme HerryView Answer on Stackoverflow
Solution 6 - Mavendr0iView Answer on Stackoverflow
Solution 7 - MavenStevenView Answer on Stackoverflow