简体   繁体   中英

How to include integration tests via maven-surefire-plugin using path

My Java Maven project separates unit tests from integration tests in the directory structure:

  • Unit tests under src/test/java ;
  • Integration tests src/integration-test/java .

src/integration-test/java is a non-default test source directory, so I added it manually to the project using build-helper-maven-plugin , as you can see:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
            <execution>
                <id>add-integration-test-sources</id>
                <phase>generate-test-sources</phase>
                <goals>
                    <goal>add-test-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>src/integration-test/java</source>
                    </sources>
                </configuration>
            </execution>
            ...
        </executions>
    </plugin>

I also used maven-failsafe-plugin to include the test integration classes in the test execution flow, as shown below.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.0.0-M4</version>
        <configuration>
            <includes>
                <include>**/*IntegrationTest.java</include>
            </includes>
        </configuration>
        <executions>
            <execution>
                <id>integration-tests</id>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
                <configuration>
                    ...
                </configuration>
            </execution>
        </executions>
    </plugin>

This approach works, but obliges me to use a naming convention in the test classes. Precisely, only the ones ending with "IntegrationTest" will be executed.

I would like to configure the plugin based on a naming convention in the path and not in the file name. Precisely, I intend to allow all classes under src/integration-test/java regardless the file names. I did not succeed so far and every tutorial in the web only shows the approach I implemented and showed you above.

Does anyone have any suggestion on how to do that?

Thanks

AFAIK, both plugins (surefire and failsafe) simply use the classpath. So, to achieve what you want, you either have to use a naming convention (eg Test vs IT as is standard) or use two different modules.

The only other (really ugly) solution I could find would be:

  • put the normal unittests also in another directory
  • create two profiles
    • one for surefire with the corresponding source directory in build-helper
    • a second for failsafe with the corresponding source directory in build-helper
  • in each profile, you deactivate the other test plugin via it's configuration
  • make the surefire profile activeByDefault
  • in integration-test phase execute an invoker on the current pom deactivating the surefire profile and activating the other one

This might work, but is hell to maintain and code ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM