简体   繁体   English

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

[英]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:我的 Java Maven 项目在目录结构中将单元测试与集成测试分开:

  • Unit tests under src/test/java ; src/test/java下的单元测试;
  • Integration tests src/integration-test/java .集成测试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: 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>

I also used maven-failsafe-plugin to include the test integration classes in the test execution flow, as shown below.我还使用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>

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.准确地说,只会执行以“IntegrationTest”结尾的那些。

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.准确地说,我打算允许src/integration-test/java下的所有类,而不管文件名。 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. AFAIK,两个插件(surefire 和 failsafe)都只使用类路径。 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.因此,要实现您想要的目标,您要么必须使用命名约定(例如标准测试与 IT),要么使用两个不同的模块。

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一个用于在 build-helper 中使用相应源目录的surefire
    • a second for failsafe with the corresponding source directory in build-helper使用 build-helper 中相应的源目录进行故障安全的第二个
  • in each profile, you deactivate the other test plugin via it's configuration在每个配置文件中,您通过其配置停用其他测试插件
  • make the surefire profile activeByDefault使 Surefire 配置文件 activeByDefault
  • in integration-test phase execute an invoker on the current pom deactivating the surefire profile and activating the other one在集成测试阶段,在当前 pom 上执行一个调用程序,停用 Surefire 配置文件并激活另一个

This might work, but is hell to maintain and code ;)这可能有效,但维护和编码是地狱;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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