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

Cucumber 2.0+测试结果不显示Examples名称及步骤问题求助

解决Cucumber 2.0+结合JUnit时Examples名称不显示的问题

我之前也碰到过一模一样的情况,尤其是在Serenity BDD的Screenplay模式项目里用Scenario Outline的时候,JUnit控制台输出总在刷Scenario Outline的名字,完全看不到Examples里的具体用例标识,排查下来可以从这几个方向调整:

1. 先搞定依赖版本兼容性

Cucumber 2.x之后的版本和Serenity BDD的适配有严格要求,别随便混版本。比如如果用Serenity 4.x系列,对应的Cucumber最好选7.x版本,你可以把依赖调整成这样:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>7.14.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>7.14.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>net.serenity-bdd</groupId>
    <artifactId>serenity-cucumber7</artifactId>
    <version>4.0.34</version>
    <scope>test</scope>
</dependency>

重点是统一Cucumber的版本号,别让cucumber-java和cucumber-junit版本不一致,不然很容易出奇怪的兼容问题。

2. 配置JUnit运行器的输出插件

默认的JUnit输出太简略,你可以在测试Runner类里加上Cucumber的pretty插件,强制输出更详细的内容:

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources/features",
        glue = "你的步骤定义包路径",
        plugin = {"pretty", "junit:target/cucumber-reports/Cucumber.xml"},
        monochrome = true
)
public class CucumberTestRunner {
}

pretty插件会让控制台输出显示每个Examples行的参数和对应的步骤,monochrome = true还能避免输出乱码,让内容更清晰。

3. 调整Serenity的配置文件

在src/test/resources/serenity.properties里加这两个配置,让Serenity在实时输出里也带上Examples的信息:

serenity.test.show.step.details=true
serenity.report.show.scenario.outline.examples=true

这俩配置不仅能让最终报告显示Examples详情,JUnit运行时的实时输出也会看到每个Examples行的独特标识。

4. 检查Feature文件的写法规范

确保你的Scenario Outline和Examples写法是标准的,比如:

Scenario Outline: 添加不同类型的待办事项
    Given 我打开了TODO页面
    When 我添加待办事项 "<todoItem>"
    Then 待办事项列表里应该有 "<todoItem>"

    Examples:
        | todoItem       |
        | 买牛奶         |
        | 写代码文档     |
        | 打扫房间       |

Examples表格要有明确的列名,占位符和表格列要对应上,不然Cucumber识别不出每个Examples行的独特性,自然只会重复输出Scenario Outline的名字。

按这几步调整完,再跑JUnit测试,应该就能在实时输出里看到类似“添加不同类型的待办事项 #1: 买牛奶”这种带Examples标识的内容了,不会再一直刷同一个名字。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:52:06