【问题标题】:Maven + Surefire return code is 0 on failed testsMaven + Surefire 在失败的测试中返回代码为 0
【发布时间】:2013-02-12 10:00:49
【问题描述】:

我有一个项目,测试分为单元和集成阶段。我让它运行 buildbot,问题是即使在测试中失败,maven 返回代码也是 0,所以 buildbot 构建成功。

这是 mvn integration-test 的结果:

Results :

Tests in error: 
 Info about failed tests

Tests run: 5, Failures: 0, Errors: 5, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 minute 19 seconds
[INFO] Finished at: Tue Feb 12 09:43:53 UTC 2013
[INFO] Final Memory: 36M/97M
[INFO] ------------------------------------------------------------------------

$ echo $?
0

没有构建成功部分的 mvn install 的结果是相同的 结果:

Tests in error: 
  Info about failed tests

Tests run: 5, Failures: 0, Errors: 5, Skipped: 0

$ echo $?
0

Surefire 的配置是这样的:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.13</version>
  <configuration>
    <printSummary>true</printSummary>
    <excludedGroups>com.testlib.IntegrationTest</excludedGroups>
  </configuration>
  <executions>
    <execution>
    <id>unit-tests</id>
    <phase>test</phase>
    <goals>
      <goal>test</goal>
    </goals>
    <configuration>
          <excludedGroups>com.testlib.IntegrationTest</excludedGroups>
    </configuration>
    </execution>
    <execution>
    <id>integration-tests</id>
    <phase>integration-test</phase>
    <goals>
      <goal>test</goal>
    </goals>
    <configuration>
      <includes>
            <groups>com.testlib.IntegrationTest</groups>
      </includes>
        </configuration>
    </execution>
  </executions>
</plugin>

我已阅读有关 maven 返回码的其他线程,但理论上相关的错误应该在我的 maven 版本中修复(Apache Maven 2.2.1 (rdebian-8))

有没有办法改变这种行为?

更新: 正如建议的那样,我尝试了surefire:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.13</version>
    <dependencies>
        <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit47</artifactId>
        <version>2.13</version>
        </dependency>
    </dependencies>
    <configuration>
        <groups>com.testlib.IntegrationTest</groups>
    </configuration>
    <executions>
        <execution>
            <goals>
            <goal>integration-test</goal>
            <goal>verify</goal>
            </goals>
            <configuration>
                <includes>
                  <include>**/*.class</include>
                </includes>
            </configuration>
        </execution>
    </executions>

我需要 surefire-junit 以避免初始化错误。

【问题讨论】:

    标签: maven surefire


    【解决方案1】:

    首先检查是否有相同的父pom配置:

    <testFailureIgnore>true</testFailureIgnore>
    

    某处...您可以通过以下方式查看:

    mvn help:effective-pom
    

    此外,您正在尝试使用 maven-surefire-plugin 运行集成测试,这是完全错误的。对于集成测试,请使用 maven-failsafe-plugin。另一件事是以正确的方式命名您的集成测试,例如 IT*.java、*IT.java 等。

    另一件事是你为什么使用这么旧的 Maven 版本检查 Maven 3.0.4。

    啊对不起。监督您正在谈论集成测试。如果您正确使用 maven-failsafe-plugin 进行集成测试,它包含一个特定目标 verify 其目的是在之后检查集成测试的结果。但是您需要通过执行块单独配置它并绑定到特定的生命周期阶段。

    【讨论】:

    • 不,我没有 testFailureIgnore,我会尝试故障保护
    • 啊...我的误解,基于您使用的集成测试需要单独配置,正如我在上一段中所写的那样。
    • 您是否正确配置了故障保护等。您是否使用验证目标?
    • 包含 .class 文件没有意义,因为包含规则适用于源文件而不是编译文件。
    • 如果我删除它,我会得到 ------------------------------------ ------------------- T E S T S ------------------ ------------------------- 没有要运行的测试。结果:测试运行:0,失败:0,错误:0,跳过:0,我可以将其更改为 * * / * .java 然后我得到失败的测试和 0 retcode,但我认为这不是重点
    【解决方案2】:

    我设法让它在两种不同的配置下工作,使用组和使用文件夹,只使用surefire

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.13</version>
        <configuration><excludedGroups>com.testlib.IntegrationTest</excludedGroups>
        </configuration>
        <executions>
            <execution>
                <id>integration-test</id>
                <phase>integration-test</phase>
                <goals>
                    <goal>test</goal>
                </goals>
                <configuration>
                  <skip>false</skip>
                          <excludedGroups>com.biicode.testlib.UnitTest</excludedGroups>
                          <groups>com.testlib.IntegrationTest</groups>
                </configuration>
            </execution>
        </executions>
    
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <excludes>
                <exclude>**/integration/*.java</exclude>
            </excludes>
        </configuration>
        <executions>
        <execution>
            <id>integration-test</id>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
            <skip>false</skip>
            <excludes>
                <exclude>none</exclude>
            </excludes>
            <includes>
                <include>**/integration/*.java</include>
            </includes>
        </configuration>
      </execution>
      </executions>
    </plugin>
    

    【讨论】:

      猜你喜欢
      • 2015-08-09
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      • 2011-03-22
      • 1970-01-01
      • 2014-09-18
      • 2023-03-16
      相关资源
      最近更新 更多