Skip integration tests from maven deploy

Here’s a small problem I had to deal with recently:

How can I exclude integration-tests when executing mvn install or mvn deploy  ? A colleague of mine just asked me this precise question so I though I could drop a few lines here.

Maven executes all phases up to the phase specified in the mvn phase command, including that phase. A list of these phases is described on this page.

  1. validate
  2. generate-sources
  3. process-sources
  4. generate-resources
  5. process-resources
  6. compile
  7. process-classes
  8. generate-test-sources
  9. process-test-sources
  10. generate-test-resources
  11. process-test-resources
  12. test-compile
  13. test
  14. prepare-package (maven 2.1+)
  15. package
  16. pre-integration-test
  17. integration-test
  18. post-integration-test
  19. verify
  20. install
  21. deploy

So if I run mvn install or mvn deploy  the integration tests will be executed.  This can take a very long time and I might want to skip it, especially if this is done anyways on a continuous integration server.

So this is how you can do this.

First define a new profile in your pom.xml, let’s call it integration-tests.

<profiles>
   <profile>
       <id>integration-tests</id>
        ....
   </profile>
   ....
</profiles>

We want this profile to be activated by default but let the user the possibility to disable it. We do this by specifying that the profile will be active if a property named skipIntegrationTests is missing. Which means that if the property exists, then the integration tests will not be activated. 

<profiles>
   <profile>
       <id>integration-tests</id>
       <activation>
          <property>
               <name>!skipIntegrationTests</name>
          </property>
        </activation>
     ....
   </profile>
  ....
</profiles>

Now all we need to do is to move all code related to integration tests in this profile. Here is how it looks like: 

   <profile>
       <id>integration-tests</id>
          <dependencies>
            <!-- Dependencies used only by the integration tests -->
          </dependencies>
          <activation>
              <property>
                   <name>!skipIntegrationTests</name>
             </property>
        </activation>
        <build>
           <plugins>
              <plugin>
              <artifactId>my-plugin-for-pre-integration-test</artifactId>
                  <executions>
                     <execution>
                        <id>configure-platform-for-integration-tests</id>
                        <phase>pre-integration-test</phase>
                    .....
                    </execution>
             </plugin>
             <plugin>
                <artifactId>my-plugin-used-integration-test</artifactId>
                <executions>
                     <execution>
                        <id>configure-platform-for-integration-tests</id>
                        <phase>pre-integration-test</phase>                    .....
                    </execution>
             </plugin>

             <plugin>
                <artifactId>my-plugin-for-post-integration-test</artifactId>
                <executions>
                     <execution>
                        <id>configure-platform-for-integration-tests</id>
                        <phase>post-integration-test</phase>                    .....
                   </execution>
             </plugin>
          ....
         </plugins>
      </build>
</profile>

Now you only need to run

    mvn deploy -DskipIntegrationTests

And remember to run your integration tests once in a while.

Advertisements
This entry was posted in testing and tagged , , , . Bookmark the permalink.

4 Responses to Skip integration tests from maven deploy

  1. Sachin Agrawal says:

    Thanks Emil

  2. Donnie T says:

    Thank you for this. Nice and simple and works first time.

  3. bar says:

    Another way is that you can pass in parameters to mvn command, -DskipITs skips the integration tests, -DSkipTests skips both UT and IT, see here: https://maven.apache.org/surefire/maven-failsafe-plugin/examples/skipping-test.html

  4. Emmad says:

    Thanks. Its working great 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s