如何使用路径通过 maven-surefire-plugin 包含集成测试

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

我的Java Maven项目在目录结构中将单元测试与集成测试分开:

src/integration-test/java是一个非默认的测试源目录,所以我使用build-helper-maven-plugin手动添加到项目中,可以看到:

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

我还使用 maven-failsafe-plugin 将测试集成 类 包含在测试执行流程中,如下所示。

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

这种方法可行,但我不得不在测试中使用命名约定 类。准确的说,只会执行以"IntegrationTest"结尾的

我想根据路径中的命名约定而不是文件名中的命名约定来配置插件。准确地说,我打算允许 src/integration-test/java 下的所有 类 而不管文件名。到目前为止我没有成功,网络上的每个教程都只展示了我实现并在上面向您展示的方法。

有人对如何做到这一点有什么建议吗?

谢谢

据我所知,两个插件(surefire 和 failsafe)都只使用类路径。因此,要实现您想要的效果,您要么必须使用命名约定(例如标准的测试与 IT),要么使用两个不同的模块。

我能找到的唯一其他(非常难看)解决方案是:

  • 将普通单元测试也放在另一个目录中
  • 创建两个配置文件
    • 在build-helper中对应源码目录的surefire
    • 使用构建助手中相应的源目录进行故障保护一秒钟
  • 在每个配置文件中,您通过它的配置停用另一个测试插件
  • 将 surefire 配置文件设置为 activeByDefault
  • 在集成测试阶段,在当前 pom 上执行一个调用程序,停用 surefire 配置文件并激活另一个

这可能有效,但维护和编码非常困难 ;)