How to run individual test in the integration-test

2019-01-31 07:16发布

问题:

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> 
    ..... 

回答1:

If you're using the Maven failsafe plugin, you can run a single integration test by setting the it.test property to your fully qualified test class name:

mvn -Dit.test=your.TestCase verify

See the failsafe plugin docs for more info.



回答2:

The Failsafe documentation would have you specify the test like so:

mvn -Dit.test=BrokenIT verify

However, -Dit.test does not appear to work any longer. Rather, the same parameter used to specify a Surefire test will apparently work for Failsafe as well. For example:

mvn -Dtest=WorksIT verify

I've filed a bug (EDIT: which was closed as "Cannot Reproduce" in 2.12) to correct the documentation.



回答3:

I struggled through this, and I created an additional profile to use when I wanted to run just one integration test. I hope that I've successfully extracted just the right part here:

    <profile>
        <id>integrationTestSingle</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>surefire-it</id>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                            <configuration>
                                <includes>
                                    <include>**/integration/**/${test}.java</include>
                                </includes>
                                <skipTests>false</skipTests>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <argLine>-Xms256M -Xmx768M -XX:MaxPermSize=256M</argLine>
                    </configuration>
                </plugin>

                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>default-test</id>                                
                            <configuration>
                                <skipTests>true</skipTests>
                            </configuration>
                        </execution>
                    </executions>

                </plugin>
            </plugins>
        </build>
    </profile>

Now, I call maven with the integrationTestSingle profile and with -DfailIfNoTests=false -Dtest=NameOfTest, and it doesn't run any of the regular tests during the regular "test" phase, and runs just the NameOfTest test during the integration-test phase.



回答4:

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



回答5:

just add -DfailIfNoTests=false works for me with testNG. Something like this:

mvn integration-test -Dtest=aITest -DfailIfNoTests=false


回答6:

Just ran into this myself. Something like this worked well for me:

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

The trick was to ensure that the test-group was mentioned before the -Dtest portion.