2

Is it possible not to run some of the JUnit test cases by default at build time?

Goal:

We have two kinds of test cases:

  • "Unit Tests"
  • "Integration Tests" (tests the full path up to DB)

On build server, we do not want integration tests to be run (DB not available, etc.). However, unit tests should be run. Currently we achieve this by keeping the integration tests commented out in CM and enabled by developers as needbed.

This is a cumbersome arrangement. What we would like to have is as a default, tell maven to run only unit tests. One way this can be done I suppose is to keep integration tests in a separate package which is not part of default build. However, this will keep test target code and test cases physically separate and typically go out of sync over time.

Any good solutions?

gammay
  • 5,957
  • 7
  • 32
  • 51

1 Answers1

2

You could use different profiles in pom. In default you exclude integrationtests.

<plugin>
     <artifactId>maven-surefire-plugin</artifactId>
     <groupId>org.apache.maven.plugins</groupId>
     <version>2.9</version>
     <configuration>
         <skip>false</skip>
         <useFile>false</useFile>
         <argLine>-Xms1024m -Xmx1024m -XX:MaxPermSize=512m</argLine>
         <excludes>
             <exclude>**/*IntegrationTest.java</exclude>
         </excludes>
     </configuration>
</plugin>

<profiles>
    <profile>
        <id>integration-test-builder</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <groupId>org.apache.maven.plugins</groupId>
                    <version>2.9</version>
                    <configuration>
                        <skip>false</skip>
                        <useFile>false</useFile>
                        <argLine>-Xms1024m -Xmx1024m -XX:MaxPermSize=512m</argLine>
                        <excludes>
                            <exclude>none</exclude>
                        </excludes>
                        <includes>
                            <include>**/*IntegrationTest.java</include>
                        </includes>
                    </configuration>
                    <executions>
                        <execution>
                            <id>integration-tests</id>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>    
</profiles>

To run integrationstest you simple do: mvn clean integration-test -P integration-test-builder

Henrik
  • 1,797
  • 4
  • 22
  • 46
  • +1 for quick and descriptive reply. A related question: we have multi-module POMs. Is there a good way to put the above plugin/profile definition in a common POM like test-configuration-pom.xml and then include this POM in all other project POMs? – gammay Apr 08 '14 at 15:12
  • Yes you could use it as a parent pom and include it in all Child poms. – Henrik Apr 08 '14 at 15:22