0

I have a multi module maven project with a structure

parent
  pom.xml
module
  pom.xml
  core-api
    pom.xml
  integ-tests
    pom.xml

I have maven surefire plugin setup for executing the unit tests '*Test.java' which are houses in the 'core-api' module.

We have slow long-running integration tests housed in a separate 'integ-tests' module. we use '*Test.java' for our integ tests as well.

We need to be able to compile all source code but want to exclude the 'integ-test' from running as part of the default maven 'test' phase. We plan to use a profile to enable the test phase of the 'integ-test' module. I don't want to use the 'failsafe' plugin.

A matrix outlining the combination is here

mvn              | core           | integ-test
test             | run unit tests | exclude
test -PintegTest | unit tests     | integ tests 

I've defined the surefire plugin in my parent pom, with a property 'skip.integ.tests' which will be controlled via a profile '-PintegTests'.

<properties>
  <skip.integ.tests>true</skip.integ.tests>
</properties>
..
<build>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
  </plugin>
</build>
..
<profiles>
  <profile>
    <id>integTests</id>
    <properties>
      <skip.integ.tests>false</skip.integ.tests>
    </properties>
  </profile>
</profiles>

In my 'integ-test' pom, i've then overridden the 'maven-surefire-plugin' config and have the 'skipTests' configuration set to look at the value of the property.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <skipTests>${skip.integ.tests}</skipTests>
      </configuration>
    </plugin>
  </plugins>
</build>

My problem is the integ-test module tests run in every case. Any ideas on where i'm going wrong with the setup?

Attila
  • 28,265
  • 3
  • 46
  • 55
emeraldjava
  • 10,894
  • 26
  • 97
  • 170
  • "I don't want to use the 'failsafe' plugin.". Why not? It does what you want in a much simpler way. Just name your integration tests FooITs, and then you can use -DskipITs to disable them. (Or if you insist on a profile, have a profile that set the equivalent property) – ptyx Oct 25 '13 at 16:51
  • Related: [maven: How can I skip test in some projects via command line options?](https://stackoverflow.com/questions/9123075/maven-how-can-i-skip-test-in-some-projects-via-command-line-options) – Vadzim Sep 15 '19 at 10:41

2 Answers2

1

First you should name your integration tests accordingly to the naming conventions of the maven-failsafe-plugin which is intended to run the integration tests. Furthermore the pre-integration-test, integration-test and post-integration-test life cycle phases are intended for running those tests. This means in your case to configure the maven-failsafe-plugin accordingly to the documentation like this. The maven-failsafe-plugin bounds to the integration-test life cycle phase.

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.16</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

I would suggest to add the following profile into your integration test module like this:

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

This gives you simply the following options:

  • mvn test

    running unit tests only

  • mvn -DskipTests=true test

    running compilation etc. but no unit tests.

  • mvn -Prun-its verify

    Running packaging etc. unit tests and integration tests

  • mvn install

    Running installation without integration tests.

  • mvn -DskipTests=true install

    Running installation without running unit tests nor integration tests.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • thanks and i do hear you but i did make it clear i can't use failsafe as part of the solution. i'm in a situation where we have some brittle integration tests but we cannot allow an integration test failing to block a release, so i need some workaround where by the integration tests are only run in specific circumstances – emeraldjava Oct 25 '13 at 16:49
  • Which i exactly gave you. Just in case of the activated profile `run-its` the integration tests where running otherwise they don't. (See [here](https://github.com/khmarbaise/maui/tree/master/src/main/resources/it-example-container-selenium) for full example in the mod-it). – khmarbaise Oct 25 '13 at 16:55
1

In your Maven build, you can exclude:

  • compilation and execution of both unit tests (by Surefire plugin) and integration tests (by Failsafe plugin) by adding -Dmaven.skip.test=true
  • execution of both unit and integration tests via -DskipTests
  • execution of integration tests via -DskipITs

Instead, if you have your integration tests in a separate Maven module (i.e. integ-test in your case), you can directly exclude that from the Maven build via profile, like in below example -- see extract of aggregator's pom.xml and maven command line to be launched:

<modules>
  <!-- remove 'integ-test' from this list -->
</modules>
<profiles>
    <profile><id>build-it</id>
        <activation><activeByDefault>true</activeByDefault></activation>
        <modules><module>integ-test</module></modules>
    </profile>
</profiles>

and then mvn install -P !build-it

PaoloC
  • 3,817
  • 1
  • 23
  • 27