How to run individual test in the integration-test target in maven

Viewed 39304

We have a hundreds of tests defined for our integration-test phase lifecycle in maven, and they take a long time to finish.

What I want to do is run just one test in the integration-test. I tried doing :

mvn -Dtest=<my-test> integration-test

but that does not work. The -Dtest runs only the tests in the unit test goal, not the integration-test phase. I tried the -Dintegration-test=<my-test> instead, and that was ignored.

Is there a way to do that ?


My configuration is:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <executions> 
        <execution> 
            <id>surefire-it</id> 
            <phase>integration-test</phase> 
            <goals> 
                <goal>test</goal> 
            </goals> 
            <configuration> 
                <excludes> 
                    <exclude>none</exclude> 
                </excludes> 
                <includes>
                    <include>**/api/**</include> 
                </includes> 
    ..... 
9 Answers

I'm not sure about JUnit, but for TestNG the strategy would be to define a suite XML file with only the one test, and then in your POM configure the surefire plugin to only run that. In your POM, you would have something like this (disclaimer, this is untested):

  <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <executions>
      <execution>
        <phase>integration-test</phase>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>single-test.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

To configure the suite file, see http://testng.org/doc/documentation-main.html

I am using: Apache Maven 3.6.3

openjdk version "11.0.2" 2019-01-15

<groupId>org.codehaus.mojo</groupId>
<artifactId>failsafe-maven-plugin</artifactId>
<version>2.4.3-alpha-1</version>

This command worked for me:

mvn failsafe:integration-test  -Dsurefire.skip=true -DskipIntegrationTests=false -DskipTests=false -Dtest=com.myxyz.func.ITestGate

Was actually simpler that the answers above by going back to basics with the actual documentation.

Running Junit 5 integration tests:

openjdk version "11.0.11" 2021-04-20
Apache Maven 3.6.3

In the main build just drop in documented failsafe config:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.0.0-M5</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

Then you can run only a specific integration test with:

mvn -Dtest=\*cs1_\* verify

Note that this version will run your tests in the target folder and if you need to load any external files that are something like src\test\resources\x.y then they are copied to target\test-classes\x.y

This works for me, when I gonna run only one test method in integration test

    mvn clean -Dit.test=some.package.SomeTestClass#testMethodName integration-test
Related