Search code examples
javajenkinscontinuous-integrationintegration-testing

How to make jenkins run integration tests only when deploying to UAT?


I'm using jenkins and I have a staging > qa > uat > master environment set. I don't want jenkins to run any integration tests in staging and qa environments, only for uat and master.

Is it possible to do that? If so, how? I mean, should I separate unit and integration tests in different packages? Today I have:

src/main/java

src/test/java

Solution

  • Define a interface IntegrationTest

        public interface IntegrationTest {}
    

    And use the annotation @Category from Junit4 for categorizing your tests as integraiton test.

    @Category(IntegrationTest.class)
    public class YourTest
    

    So use the maven surefire and failsafe plugin to create groups of testes.

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <excludedGroups>you.package.IntegrationTest</excludedGroups>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <includes>
                        <include>**/*.java</include>
                    </includes>
                    <groups>you.package.IntegrationTest</groups>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    The Integration test will be automatically excluded from the default execution i.e mvn clean package. However, if you run mvn integration-test all the integration tests will run.