配置 maven-failafe-plugin 以查找不在 src/test/java 中的集成测试

2022-09-03 15:29:04

我的目录结构是这样的:

  • src/integrationTest/java
  • src/test/java
  • src/main/java

我试图获得故障安全来拾取集成测试,但未能以我想要的方式做到这一点。

我试过这个:

<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.17</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <testSourceDirectory>src/integrationTest/java</testSourceDirectory>
        <testClassesDirectory>${project.build.directory}/it-classes</testClassesDirectory>
    </configuration>
</plugin>

和这个:

<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.17</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <includes>
            <include>src/integrationTest/**/*.java</include>
        </includes>
    </configuration>
</plugin>

无济于事;故障安全找不到要运行的测试。

我已经能够使用build-helper-maven插件来添加测试源目录,然后故障安全运行测试。

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

但是,现在的问题是,surefire 现在也将测试作为单元测试运行。此外,当故障安全应该能够找到测试时,似乎没有必要使用另一个插件;我宁愿不需要使用构建助手maven插件并配置sururefire以排除该路径。

我做错了什么?


答案 1

默认情况下,故障安全配置为仅包括 IT*.java、*IT.java 或 *ITCase.java。同时,仅编译来自测试源的测试源。您需要修改这两种行为。src/test/java

  1. 用于添加为 测试源,以便自动拾取。(您在上次尝试时已经完成此操作。build-helper-maven-pluginsrc/integationTest/javamaven-compiler-plugin

  2. 直接排除集成测试(请参阅下面的示例)或仅包含非集成测试(请参阅默认包含)。maven-surefire-plugin

  3. 直接仅包含集成测试,而不是默认包含。maven-failsafe-plugin


<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.17</version>
  <configuration>
    <excludes>
      <exclude>**/*Stuff.java</exclude>
    </excludes>
  </configuration>
</plugin>
<plugin>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.17</version>
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <includes>
      <include>**/*Stuff.java</include>
    </includes>
  </configuration>
</plugin>

事实上,使用可能也适用于限制每个测试插件的范围,但是您必须进行更改以将类输出到不同的文件夹,也许将其执行分成两部分,因此可能不值得付出努力。testClassesDirectorymaven-compiler-plugintest-compile


答案 2

在Groovy上进行了工作集成测试,并使用此配置:src/test/groovy

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.0.0-M5</version>
    <executions>
        <execution>
            <id>spock-integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <failIfNoTests>true</failIfNoTests>
                <testSourceDirectory>${project.basedir}/src/test/groovy</testSourceDirectory>
                <includes>
                    <include>**/*Specification</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>

请注意,中的模式不包含文件扩展名 ( 不起作用)<include>**/*Specification.groovy


推荐