【问题标题】:cucumber.json report getting overwritten by rerun scenario reportcucumber.json 报告被重新运行方案报告覆盖
【发布时间】:2018-05-01 11:37:55
【问题描述】:

我有 UI 测试项目和一个具有相同技术堆栈(JAVA1.8、Cucumber-JVM、JUnit、Maven)的 API 测试项目,这两个项目都向我展示了这个问题。可能是因为两者都存在相同的依赖集。

我采用了 Flaky 测试重新运行机制,使用 maven-surefire-plugin 内置功能 <rerunFailingTestsCount>1</rerunFailingTestsCount>。另外,我添加了基于<groupId>io.cucumber</groupId> 而不是<groupId>info.cukes</groupId> 的黄瓜依赖项。这两个都有自己版本的 cucumber-java 和 cucumber-jvm 依赖项。

我的 POM.XML 看起来像这样。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <configuration>
         <rerunFailingTestsCount>1</rerunFailingTestsCount>
    </configuration>
    <executions>
         <execution>
             <id>acceptance-test</id>
             <phase>integration-test</phase>
             <goals>
                 <goal>test</goal>
             </goals>
             <configuration>
                 <forkCount>1</forkCount>
                 <reuseForks>true</reuseForks>
                  <includes>
                      <include>**/*Runner.class</include>
                  </includes>
              </configuration>
         </execution>
    </executions>
</plugin>
<dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-jvm</artifactId>
        <version>2.4.0</version>
        <type>pom</type>
    </dependency>

仅运行器文件代码

@RunWith(Cucumber.class)
@ContextConfiguration(locations = {"file:/src/test/resources/spring-config.xml"})
@CucumberOptions(
        glue = "com.test.uitest",
        features = "classpath:cucumber",
        tags = {"~@ignore","@ui_home"},
        monochrome = true,
        plugin = {"pretty", "html:target/cucumber-reports",
                "json:target/cucumber-reports/cucumber.json",
                "rerun:target/rerun.txt"} //Creates a text file with failed scenarios
)
public class AllTestsRunner {
}

现在显然,我需要另一个运行器,其中包含以下代码(根据 StackOverflow 上的其他论坛和线程)

@RunWith(Cucumber.class)
@CucumberOptions(
        monochrome = true,
        glue = "com.test.uitest",
        features = "@target/rerun.txt", //Cucumber picks the failed scenarios from this file
        format = {"pretty", "html:target/rerun-reports",
                "json:target/cucumber-reports/rerun-report.json"}
)
public class FailedTestsRunner {
}

但我不需要这个第二个跑步者,因为只有一个跑步者在上面,重新运行机制绝对出色。甚至,不需要在 1st runner 中生成 rerun.txt 文件。maven-surefire 插件(v_2.21.0)中的内置机制以及 io.cucumber v_2.4.0 可以完美运行,如果在第一次执行期间出现任何场景失败,它会自动重新运行,而不会将其记录在 rerun.txt 文件中。

## 问题是##

我的功能文件中有 5 个场景。如果他们都在第一次运行中通过。它成功生成了带有 cucumber.json 报告的报告,显示了所有 5 个场景。 但是,如果(比如说)5 个场景中有 2 个失败,并且这些场景在重新运行机制中自动执行,那么 cucumber.json 报告文件只记录这两个场景的结果,而不是所有 5 个场景的结果。如果这 2 个场景在重新运行中通过,则整体构建通过;如果这 2 个场景失败,则整体构建失败。这是正确的,但我的问题是 cucumber.json 被重新运行机制覆盖。 我尝试使用 maven-cucumber-reporting 插件 v_3.16.0 但它实际上读取 cucumber.json 文件本身,因此不能解决我的问题。任何帮助,将不胜感激。

【问题讨论】:

    标签: java maven cucumber cucumber-jvm test-reporting


    【解决方案1】:

    好消息是你没有做错任何事!

    坏消息是您观察到的结果完全符合预期。这是链接不同工具 (Surefire --&gt; JUnit --&gt; Cucumber) 的自然结果,这些工具原本不知道彼此。从 Cucumber 的角度来看,重新运行似乎是一个全新的执行,因此它会很高兴地覆盖旧报告。只有在链的开始,才有可能创建准确的报告。

    因此,您的选择从最少到最多,从最差到最好质量是:

    1. 使用 Surefire 生成的报告。

    2. 编写您自己的插件来附加结果而不是覆盖它们。

    3. 参与 Cucumber 项目并帮助从根本上解决这个问题。

    编辑:删除了使用 cucumber-jvm-parallel-plugin 为每个场景创建单独的单元测试的建议。这实际上是行不通的。

    【讨论】:

    • 嗨,mpkorstanje,我尝试使用带有版本5.0.0cucumber-jvm-parallel-plugin 以及版本为io.cucucmber 的版本2.4.0,使用&lt;parallelScheme&gt;SCENARIO&lt;/parallelScheme&gt; 但我遇到的问题是,对于具有5 的功能场景,它创建 5 个跑步者,这 5 个跑步者中的每一个都将重复运行所有 5 个场景。这是一个错误还是我做错了什么。
    • 另外,另一个问题是,如果任何场景失败(由于自动化代码问题),它会重新运行并再次失败。需要修复的合法故障。但构建显示 BUILD SUCCESS 而不是失败。这不是正确的行为吗?你有什么建议。
    • 我以前听人报告过这个问题,但每次我以 github repo 的形式请求 mcve 时,它​​都会消失。 stackoverflow.com/help/mcve
    【解决方案2】:

    在这种情况下,AllTestsRunner 会重复运行,直到它成功达到rerunFailingTestsCount 参数中提到的计数或在上次运行时失败。所以最后一次运行总是获胜。

    surefire 插件在 target 文件夹中创建自己的报告。两个报告是AllTestsRunner.txt,这是一个摘要,TEST-AllTestsRunner.xml 有详细信息。这两个报告都有关于所有运行的详细信息。您可以创建一个自定义程序来将 TEST-AllTestsRunner.xml 文件转换为所需的 json。

    有一个surefire report plugin 读取上面的xml文件并生成html报告。它将在站点文件夹中创建报告并使用 mvn site 运行。也许这行得通。

    【讨论】:

    • 感谢您的回复。实际上TEST-AllTestsRunner.xml的内容远远少于填写黄瓜报告(cucumber.json)所需的内容。根据maven-surefire-report-plugin,我的团队+经理对其报告不满意,他们更喜欢黄瓜报告。
    • 唯一的出路就是使用旧的重试机制,合并2个json文件。
    • 是的,我明白这一点。我已经通过使用&lt;info.cukes&gt; for cucumber-jvm 而不是&lt;io.cucumber&gt; 来使用该机制,然后结合两个json,倾向于使用最新的io.cucumber,因为它具有对重新运行机制的内置支持并且不需要有FailedTestRunner类。
    【解决方案3】:

    我面临同样的问题。我对我得到的结果很满意。在此处分享信息。

    重试而不用担心报告

    如果您只想重试失败的测试而不关心不完整的报告,这很容易。有两种方法可以做到。

    1. 使用来自 Maven 的 rerunFailingTestsCount。参考https://maven.apache.org/surefire/maven-failsafe-plugin/examples/rerun-failing-tests.html。关于报告,限制是 cucumber.json 在重新运行期间被覆盖。这意味着您只会获得重新运行的测试报告。
    2. 使用 Cucumber 的 rerun 插件。请参阅 N Riay 给出的代码。关于报告,限制是生成了两个黄瓜json文件。

    解决方案

    如果您还想获得统一报告,这里是解决方案。 在选项 2 之上,我们使用 cucumber-reporting 在所有 runner 包括 FailedTestsRunner 完成后合并两个黄瓜 json 文件。

    请参考 N Riay 给出的“ONLY RUNNER FILE CODE”部分的代码。然后,添加以下代码。 BeforeSuite 和 AfterSuite 是 TestNG 的概念。对于 Junit,情况类似。只是为了确保它只运行一次。

    import net.masterthought.cucumber.Configuration;
    import net.masterthought.cucumber.ReportBuilder;
    import net.masterthought.cucumber.reducers.ReducingMethod;
    
    @BeforeSuite
    public void beforeSuite() throws IOException {
        <Delete all the cucumber json files. Otherwise report will always combine json file of FailedTestsRunner even it's not run>
    }
    
    @AfterSuite
    public void afterSuite() {
        Configuration configuration = new Configuration(new File("target"), "my project");
        configuration.addReducingMethod(ReducingMethod.MERGE_FEATURES_WITH_RETEST);
        List<String> jsonFiles = <get the file path of the two json>;
        ReportBuilder reportBuilder=new ReportBuilder(jsonFiles,configuration);
        reportBuilder.generateReports();
    }
    

    MERGE_FEATURES_WITH_RETEST 参考 - https://javadoc.io/static/net.masterthought/cucumber-reporting/5.0.2/net/masterthought/cucumber/reducers/ReducingMethod.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 2016-05-14
      • 2010-11-14
      相关资源
      最近更新 更多