Search code examples
javamavenmaven-3maven-failsafe-plugin

How to skip integration tests by default but still run them on-demand in multi-module maven project?


I have a multi module project having the following structure:

Project:    
 - module1
 - module2
 - integration-test
 - parent pom

What is the correct way of achieving the following:

  • run unit tests from all modules(except integration-test) using mvn clean install
  • run integration tests on demand(may be by using maven-failsafe plugin or via a maven profile? )
  • fail the build when integration tests fail.
  • By Default integration tests should not be run using mvn clean install
  • integration-test module has only the integration tests.

I have tried multiple hacks using maven-failsafe plugin and maven-sunfire-plugin(for unit tests) but not able to achieve the above in standard way.

Following is how the relevant portion of integration-test pom looks like:

<dependencies>
   <!-- dependencies required for this module-->

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.12</version>
            <executions>
                <execution>
                    <id>add-integration-test-sources</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/test/java</source>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>add-integration-test-resources</id>
                    <phase>generate-test-resources</phase>
                    <goals>
                        <goal>add-test-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <filtering>true</filtering>
                                <directory>src/test/resources</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin> 
    </plugins>
</build>

<profiles>
    <profile>
        <id>run-its</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

currently when i run mvn clean install it runs integration tests too. When i run mvn -Prun-its clean verify, it is running unit tests from other modules too. what am i missing?


Solution

  • You can skip execution of the Integration tests simply by setting -DskipITs=true when running your build like so:

    mvn clean install -DskipITs=true
    

    this will run all other tests but your ITs (see here for doc).

    If you only want to run

    mvn clean install
    

    you can set the default for skipITs in your pom.xml

    <properties>
        <skipITs>true</skipITs>
    </properties>
    

    This way you can override it on demand with

    mvn clean install -DskipITs=false
    

    To run only ITs without Unittests you can configure the -Property of the maven-surefire-plugin like so

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <configuration>
            <skip>${skipUnitTests}</skip>
        </configuration>
    </plugin>
    

    so if you run

    mvn clean install -DskipITs=false -DskipUnitTests=true
    

    Note that skipUnitTests will be false by default so no need to declare a property for that.

    If you'd rather use a Profile it should work like that

        <profile>
            <id>ITs</id>
            <properties>
                <skipUnitTests>true</skipUnitTests>
                <skipITs>false</skipITs> 
            </properties>
        </profile>
    

    and run the build like so

    mvn clean install -PITs
    

    Of course you could also use the plugin-configuration for maven-surefire-plugin with true directly in the profile so there'd be no need for the extra property, like

    <profile>
        <id>ITs</id>
        <properties>
            <skipITs>false</skipITs> 
        </properties>
        <build>
           <plugins>
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.12.4</version>
                    <configuration>
                       <skip>true</skip>
                    </configuration>
                </plugin>
           </plugins>
        </build>
    </profile>