Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build parent pom and run specific integration test in Jenkins through maven?

Executing mvn clean install on my parent pom builds all the sub-modules and runs the related junit tests. It doesn't run the integration tests.

After building and running the junits, I want to run a specific integration test in a particular sub-module.

I am using following command:

mvn clean install -DTest=integrationTestName

The job fails with No Test Found error during build phase.

I also tried using

mvn clean install -DTest=integrationTestName -pl=sub-module-name

but this only builds my sub-module having integration test.

Question: How to run a single integration test of a certain module?

like image 310
Kumar Avatar asked Mar 31 '16 19:03

Kumar


2 Answers

I suppose you tried the test option (lower case) of the Maven Surefire Plugin to invoke a specific test, which Surefire couldn't find in the first module of the reactor build and hence fails.

I also suppose integration tests are executed by the Maven Failsafe Plugin. If not, they should, as mentioned by official documentation:

The Failsafe Plugin is designed to run integration tests while the Surefire Plugin is designed to run unit tests. ... If you use the Surefire Plugin for running tests, then when you have a test failure, the build will stop at the integration-test phase and your integration test environment will not have been torn down correctly. .. The Failsafe Plugin will not fail the build during the integration-test phase, thus enabling the post-integration-test phase to execute.

To make it short: it's safer and much more robust to do so.

Although the plugin configuration entry for the Maven Failsafe Plugin is also test, its corresponding command line option is it.test, so you should instead run:

mvn clean install -Dit.test=SampleIT

Where SampleIT is an integration Test (note the standard IT suffix, recognized by default by Failsafe.

The official Running a Single Test documentation also provides further details on running single integration tests.


Also note: the invocation above will work if you don't have other integration tests in other modules of the build, otherwise it will fail not finding it earlier (before hitting the concerned module).

If you are using Surefire to run the concerned integration test (again, you shouldn't) or you have several modules running integration tests, you should then configure a profile in the concerned module which would take care of this specific invocation, something like as following:

<profiles>
    <profile>
        <id>run-single-it-test</id>
        <activation>
            <property>
                <name>single.it.test</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <test>${single.it.test}</test>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

And then invoke the build as following:

mvn clean install -Dsingle.it.test=SampleIT

As such, Maven will activate the profile based on the existence of a value for the single.it.test property and pass it to the test property of the Failsafe Plugin (or the Surefire, if it was the case), Failsafe will ignore any other integration test for that execution and other modules would not suffer any impact (ignoring that property).

like image 68
A_Di-Matteo Avatar answered Sep 22 '22 16:09

A_Di-Matteo


A_Di-Matteo's answer gets you most of the way there, but you need the following config for maven-surefire-plugin to suppress all the unit tests as well.

<profiles>
    <profile>
        <id>run-single-it-test</id>
        <activation>
            <property>
                <name>single.it.test</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skipTests>true</skipTests>
                        <failIfNoTests>false</failIfNoTests>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>test</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <configuration>
                        <test>${single.it.test}</test>
                        <failIfNoTests>false</failIfNoTests>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
like image 39
Adam Avatar answered Sep 23 '22 16:09

Adam