简体   繁体   English

Surefire 重新运行失败的测试不起作用

[英]Surefire rerun failing tests not working

I want to rerun a test that I know will fail cause I am trying to test the Surefire parameter for re-running the failing tests.我想重新运行一个我知道会失败的测试,因为我正在尝试测试 Surefire 参数以重新运行失败的测试。 I tried running Maven with these two commands neither of them works as expected我尝试使用这两个命令运行 Maven 它们都没有按预期工作

-Dsurefire.rerunFailingTestsCount=2 -Dtest=TestThatFails test

and

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

Here is part of pom.xml这是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>

I was expecting that Surefire would restart the test after failure but Maven just throws this error, which I know how to solve but I want the test to be rerun.我原以为 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.

Just to add to Wim Rutgeerts's answer - rerunFailingTestsCount must be in the configuration section, not in properties , like this:只是添加到 Wim Rutgeerts 的答案 - rerunFailingTestsCount必须在configuration部分,而不是在properties ,如下所示:

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

In my case with maven-surefire-plugin 2.19.1 it worked this way.在我使用maven-surefire-plugin 2.19.1 的情况下,它是这样工作的。 When it was in properties it did not work.当它在properties它不起作用。

Although that is missing from the documentation, the parameter rerunFailingTestsCount was introduced in version 2.18 of the Maven Surefire Plugin, as mentioned in SUREFIRE-1087 .尽管文档中缺少该参数,但在 Maven Surefire 插件的 2.18 版中引入了参数rerunFailingTestsCount ,如SUREFIRE-1087 中所述 Since you're using the default version of 2.12.4 (that comes from the Super POM), that option is not available.由于您使用的是默认版本 2.12.4(来自 Super POM),因此该选项不可用。

Therefore, the fix is simply to update the Surefire version to a version that is at least 2.18;因此,修复只是简单地将 Surefire 版本更新到至少 2.18 的版本; for example, the latest, which is currently 2.19.1:例如,最新的,目前是 2.19.1:

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

Note that this parameter only works with JUnit 4+ (which is your case, since you have JUnit 4.12).请注意,此参数仅适用于 JUnit 4+(这是您的情况,因为您使用的是 JUnit 4.12)。

Instead of using the command line property -Dsurefire.rerunFailingTestsCount=2, you can also define it in the pom in the properties section除了使用命令行属性 -Dsurefire.rerunFailingTestsCount=2,您还可以在属性部分的 pom 中定义它

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

Update for JUnit 5 : Maven Surefire version 3.0.0-M4 or later now let you use rerunFailingTestsCount system property when executing JUnit 5 tests. JUnit 5更新:Maven Surefire 3.0.0-M4或更高版本现在允许您在执行 JUnit 5 测试时使用rerunFailingTestsCount系统属性。

Make sure to pass the below property when running your mvn clean stage:确保在运行mvn clean阶段时传递以下属性:

-Dsurefire.rerunFailingTestsCount=3

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

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