You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Serenity BDD+Cucumber的失败验收测试实现重试逻辑?

解决Serenity BDD 1.x + Cucumber失败场景重试不生效的问题

首先要明确:你使用的Serenity 1.9.x版本中,官方文档里的JUnit重试配置(max.retries、junit.retry.tests)仅对纯JUnit测试生效,对Cucumber场景完全不适用,而且你尝试的test.retry.count.cucumber、test.retry.count是Serenity 2.x版本才引入的配置项,在1.x里根本不被识别——这就是配置无效的核心原因。

针对你的版本,以下是可行的解决方案:

1. 用Cucumber原生重试机制配置

Serenity 1.x集成Cucumber时,需要依赖Cucumber自身的重试插件来实现失败场景重试,具体配置方式有两种:

方式一:在Serenity配置文件中添加

在serenity.properties或serenity.conf里加入:

cucumber.options=--plugin io.cucumber.core.plugin.RetryListener --retry 3
  • --retry 3表示失败场景最多重试3次
  • RetryListener是Cucumber负责重试逻辑的核心插件,必须指定

方式二:通过Maven Surefire插件传递参数

如果是用Maven运行测试,可以在pom.xml的maven-surefire-plugin中配置系统属性:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <systemPropertyVariables>
            <cucumber.options>--retry 3</cucumber.options>
        </systemPropertyVariables>
    </configuration>
</plugin>

2. 额外注意事项

  • 重试范围:这个配置只会重试失败的整个场景,不会单独重试场景中的某一步骤
  • 报告标记:重试后的场景会在Serenity测试报告中被标记为"Retried",你可以在报告里查看重试次数和结果
  • 标签过滤重试:如果只想重试特定标签的场景(比如@flaky),可以修改cucumber.options为:
    cucumber.options=--plugin io.cucumber.core.plugin.RetryListener --retry 3 --retry-tag @flaky
    

3. 验证重试是否生效

运行测试后,查看控制台输出,如果看到类似Retrying scenario: XXXX (attempt 1 of 3)的日志,说明重试逻辑已经生效;也可以查看最终生成的Serenity报告,失败场景的详情里会显示重试记录。

内容的提问来源于stack exchange,提问作者Ram

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:20:40