How to run individual test in the integration-test target in maven
Asked Answered
B

9

60

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> 
    ..... 
Berglund answered 21/5, 2009 at 19:39 Comment(4)
I've just done this on my project and it worked. Any chance you can post the surefire pluging section from the pom?Fitzpatrick
<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> .....Berglund
I can't post in the full thing because of a limitation. But that's the gist of it.Berglund
you could post as part of the question. Just out of interest what happens if you remove the excludes and includes sections. Other than that your config is the same as mineFitzpatrick
A
53

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 -D it.test=your.TestCase verify

See the failsafe plugin docs for more info.

Alyce answered 20/4, 2011 at 7:54 Comment(3)
This worked for me on 2013-07-26 with plugin version 2.15 and Maven 3.1.0 despite what the other answer says. The other answer seems to have been a bug.Number
verified for mvn 3.2.5, plugin 2.18. runs one integration test: mvn integration-test -Dit.test=MyClassIT however, this also runs unit tests: mvn verify -Dit.test=MyClassITForeplay
Be aware that this might still run unit tests. To switch off execution of unit tests add -Dtest=foo -DfailIfNoTests=false (assuming foo doesn't exist), see #6612844 .Twelfth
B
33

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.

Bunns answered 26/7, 2011 at 20:21 Comment(4)
Cannot trigger build-helper-maven-plugin which init the test environment.Sextant
@okwap That sounds like something particular to your project and plugin setup, and not a Failsafe/integration test issue.Bunns
5 years later... I had a similar problem -Dit.test, or any argument with a dot in it wouldn't work. Running the command through cmd instead of powershell fixed it for me.Siqueiros
It works for me but with a space after -DBologna
Y
6

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

mvn integration-test -Dtest=aITest -DfailIfNoTests=false
Yard answered 4/9, 2014 at 10:37 Comment(0)
P
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.

Presa answered 19/11, 2010 at 19:40 Comment(0)
O
2

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

Obeah answered 24/5, 2009 at 5:47 Comment(1)
I agree with James. TestNG is suitable for that, but I think it's better to use TestNG groups: @Test(groups = { "slow_test" }) Then in your TestNG suite: <suite name="My suite"> <test name="Slow tests"> <groups> <run> <include name="slow_test"/> </run> </groups> </test> </suite> Then you could always include/exclude particular groupsGlottalized
U
0

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.

Upstage answered 30/3, 2013 at 0:48 Comment(0)
P
0

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
Parulis answered 27/9, 2021 at 11:2 Comment(0)
E
0

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

Entreaty answered 16/10, 2021 at 7:6 Comment(0)
A
0

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
Amoebic answered 1/11, 2021 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.