【问题标题】:Surefire rerun failing tests not workingSurefire 重新运行失败的测试不起作用
【发布时间】:2016-11-24 12:00:48
【问题描述】:

我想重新运行一个我知道会失败的测试,因为我正在尝试测试 Surefire 参数以重新运行失败的测试。 我尝试使用这两个命令运行 Maven,它们都没有按预期工作

-Dsurefire.rerunFailingTestsCount=2 -Dtest=TestThatFails test

-Dsurefire.rerunFailingTestsCount=2 -Dtest=TestThatFails surefire:test

这是pom.xml的一部分

<dependency>
    <groupId>org.apache.maven.surefire</groupId>
    <artifactId>surefire-api</artifactId>
    <version>2.19.1</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.53.1</version>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>

我原以为 Surefire 会在失败后重新启动测试,但 Maven 只是抛出此错误,我知道如何解决,但我希望重新运行测试。

Results :

Tests in error: 
  testA(selenium.services.TestThatWillFail): Element is not currently visible and so may not be interacted with(..)

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 55.060 s
[INFO] Finished at: 2016-11-24T12:58:02+01:00
[INFO] Final Memory: 18M/173M

[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project eskn_selenium: There are test failures.

【问题讨论】:

    标签: java maven testing maven-surefire-plugin surefire


    【解决方案1】:

    只是添加到 Wim Rutgeerts 的答案 - rerunFailingTestsCount 必须在 configuration 部分中,而不是在 properties 中,如下所示:

    <configuration>
        <rerunFailingTestsCount>2</rerunFailingTestsCount>
    </configuration>
    

    在我的情况下,maven-surefire-plugin 2.19.1 它是这样工作的。当它在properties 中时,它不起作用。

    【讨论】:

      【解决方案2】:

      尽管文档中缺少该参数,但参数 rerunFailingTestsCount 是在 Maven Surefire 插件的 2.18 版中引入的,如 SUREFIRE-1087 中所述。由于您使用的是默认版本 2.12.4(来自 Super POM),因此该选项不可用。

      因此,修复只是将 Surefire 版本更新到至少 2.18 的版本;比如最新的,目前是2.19.1:

      <pluginManagement>
        <plugins>
          <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
          </plugin>
        </plugins>
      </pluginManagement>
      

      请注意,此参数仅适用于 JUnit 4+(这是您的情况,因为您拥有 JUnit 4.12)。

      【讨论】:

      • 哇哦,我明白了!我认为将其添加为依赖项即可完成工作,但必须将其添加为插件。我正要问你有没有注意到它已经在 pom.xml 中定义了哈哈。谢谢!
      • JUnit 5 的更新:Maven Surefire 3.0.0-M5 现在允许您在执行测试时使用 rerunFailingTestsCount 系统属性。确保在运行 mvn clean 阶段时传递以下属性:-Dsurefire.rerunFailingTestsCount=3
      • 查看 Surefire/Failsafe 的功能矩阵maven.apache.org/surefire/maven-surefire-plugin/…
      【解决方案3】:

      除了使用命令行属性-Dsurefire.rerunFailingTestsCount=2,你也可以在属性部分的pom中定义它

       <properties>
          <surefire.rerunFailingTestsCount>2</surefire.rerunFailingTestsCount>
       </properties>
      

      【讨论】:

        【解决方案4】:

        JUnit 5 更新:Maven Surefire 版本 3.0.0-M4 或更高版本现在允许您在执行 JUnit 5 测试时使用 rerunFailingTestsCount 系统属性。

        确保在运行 mvn clean 阶段时传递以下属性:

        -Dsurefire.rerunFailingTestsCount=3
        

        【讨论】:

        • 您不必传递该属性,也可以将其放入&lt;configuration&gt;,如其他答案中所述。
        最近更新 更多