【问题标题】:In Maven is it possible to keep integration tests in a separate folder from unit tests?在 Maven 中,是否可以将集成测试保存在与单元测试不同的文件夹中?
【发布时间】:2013-07-25 17:42:16
【问题描述】:

Maven and Integration Testing 页面上显示:

未来有传言称未来版本的 Maven 将支持 此外,在集成测试阶段类似 src/it/java 在测试阶段到 src/test/java。

但那是在 2011 年 12 月 11 日。这已经发生了吗?

this answer"Run maven test not in default src/test/java folder" 中提到设置<testSourceDirectory>,他们这样做是否只是为了集成测试(即integration-test 阶段)?

我希望使用 Maven FailSafe plugin 并避免重命名一堆集成测试或使用仍处于试验阶段的 JUnit @Categories

【问题讨论】:

标签: maven integration-testing maven-failsafe-plugin


【解决方案1】:

您可以像这样将 IT 放在不同的文件夹中:

.
|-- pom.xml
`-- src
    |-- it
    |   `-- java
    |       `-- com
    |           `-- soebes
    |               `-- maui
    |                   `-- it
    |                       `-- BitMaskIT.java
    |-- main
    |   `-- java
    |       `-- com
    |           `-- soebes
    |               `-- maui
    |                   `-- it
    |                       `-- BitMask.java
    `-- test
        `-- java
            `-- com
                `-- soebes
                    `-- maui
                        `-- it
                            `-- BitMaskTest.java

需要以下内容才能使编译器等知道文件夹。

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

真正运行 IT 需要以下条件:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.15</version>
  <executions>
    <execution>
      <id>integration-test</id>
      <goals>
        <goal>integration-test</goal>
      </goals>
    </execution>
    <execution>
      <id>verify</id>
      <goals>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
</plugin>

这意味着您可以在同一模块中进行集成,但缺点是运行集成测试使用与单元测试相同的资源。更好的解决方案是创建一个单独的 maven 模块,您可以在其中将集成测试放入常用文件夹 src/test/java 等,并且只配置 maven-failsafe-plugin。

【讨论】:

  • 不错!只是为了清楚测试,它将被扫描单元测试和集成测试,所以它基本上仍然依赖于文件名来区分两者对吗?
  • 第一种情况正确。但更好的是有一个不同的模块,您可以在其中更改命名约定。我不建议这样做。
  • 为什么故障安全插件在&lt;phase&gt;verify&lt;/phase&gt;而不是&lt;phase&gt;integration-test&lt;/phase&gt;期间运行?在&lt;phase&gt;post-integration-test&lt;/phase&gt; 之后运行故障安全似乎很奇怪?
  • 看起来同一个build-helper-maven-plugin调用可以和&lt;goal&gt;add-test-resource&lt;/goal&gt;一起使用,将src/it/resources添加到测试资源目录列表中。
  • @khmarbaise 在您的第一个示例中,我理解将BitMaskIT 放入名为com.soebes.maui.it 的包中,而不是BitMaskTestBitMask。我希望这些课程属于一个名为 com.soebes.maui 的课程,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2017-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
相关资源
最近更新 更多