PalBo
PalBo

Reputation: 2612

Splitting unit and integration tests with Maven

I'm setting up a CD pipeline in Jenkins and I want to run my unit tests and integration tests in two different steps. The plan is to have my pipeline script look something like this and have them run separately:

    stage('Unit tests') {
        steps {
            withMaven(maven: 'Maven 3.6.2') {
                sh 'mvn test -P coverage'
            }
        }
    }
    stage('Integration tests') {
        steps {
            withMaven(maven: 'Maven 3.6.2') {
                sh 'mvn test -P coverage'
            }
        }

I have tried using the surefire plugin as described here: https://dzone.com/articles/splitting-unit-and-integration-tests-using-maven-a, and running 'mvn test' does run only the unit test as it should, but 'mvn integration-test' runs both unit and integration tests.

I have also tried using the failsafe plugin as described here: Maven separate Unit Test and Integration Tests, but 'mvn verify' runs both unit and integration tests no matter which options I enter.

How can I make my pipeline execute the unit tests and integration tests in two different steps?

Pom with surefire:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId> <!-- surefire plugin version managed by Spring Boot -->
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
            <executions>
                <execution>
                    <id>unit-tests</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <skipTests>false</skipTests>
                        <includes>
                            <include>**/*Test.java</include>
                        </includes>
                    </configuration>
                </execution>
                <execution>
                    <id>integration-tests</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <skipTests>false</skipTests>
                        <includes>
                            <include>**/*IT.*</include>
                            <include>**/*Tests.*</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Pom with surefire and failsafe:

            <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.19.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>${surefire.skip}</skip>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

Upvotes: 5

Views: 4311

Answers (1)

PalBo
PalBo

Reputation: 2612

This solved it for me:

Below is example pom.xml which works with the following commands:

  • mvn clean verify -DskipUTs=true : Runs only integration tests (tests ending in "IT")
  • mvn clean verify -DskipITs=true : Runs only unit tests (tests ending in "Test")
  • mvn clean verify -DskipTests=true : Skips all tests

    <build>
    <finalName>test-app</finalName>
    <plugins>
    
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId> <!-- surefire plugin version managed by Spring Boot -->
            <configuration>
                <skipTests>${skipUTs}</skipTests>
            </configuration>
        </plugin>
    
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.0.0-M3</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>
    

Upvotes: 4

Related Questions