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

BDD测试场景编写困境:抽象描述VS具体示例数据?

BDD场景编写:抽象描述与具体示例的平衡策略

不用纠结二选一,这两种写法本质服务于不同阶段和目的,最佳方式是根据需求状态混合使用:

需求明确时:优先用带具体示例的场景

从测试视角出发,带具体数据的场景优势很明显:

  • 消除歧义:开发和测试能直接对齐预期结果,不会出现“新标签到底怎么展示”“其他字段会不会被修改”这类疑问;
  • 可复用性:抽离的初始数据集能在多个场景复用,还能通过遍历数据快速覆盖边界条件(比如不同类型的item、特殊格式的标签);
  • 可执行性:直接就能转化为自动化测试用例,通过状态对比快速验证功能正确性。

比如你给出的这个示例,就是典型的实用型写法:

Background:
  Given following items are in the store
    | type   | name | X  | Y    | Z    | tags |
    | single | el1  | 10 | 20   | 1.03 | t1   |
    | multi  | el2  | 10 | 20   | 30   | t2   |
    | single | el3  | 10 | 3.02 | 30   | t3   |

Scenario: Adding tag to multi-type item
  Given Edit Item Popup is opened for item: el2
  When user adds tag NEWTAG
  And user clicks on Apply changes button
  Then item store should display following items
    | type   | name | X  | Y    | Z    | tags       |
    | single | el1  | 10 | 20   | 1.03 | t1         |
    | multi  | el2  | 10 | 20   | 30   | t2, NEWTAG |
    | single | el3  | 10 | 3.02 | 30   | t3         |

需求模糊时:用抽象描述锚定核心,补充细节补全漏洞

当需求还在探索阶段,抽象行为描述能帮团队聚焦核心业务逻辑,避免过早陷入细节争议。但你提到的两个问题可以通过补充机制解决:

  • 针对“细节隐含引发开发疑问”:在抽象场景下添加补充说明或者子场景,比如在抽象场景下方加一句:> 注:新标签应追加到现有标签列表,以英文逗号分隔;其他未修改字段需保持原值不变。
  • 针对“无法探索问题空间漏Bug”:抽象场景只是起点,之后要基于核心逻辑衍生出带具体数据的测试场景,比如测试空标签、超长标签、含特殊字符的标签、单类型item加标签等边界情况。

比如先写抽象核心场景锚定需求,再补充分支场景:

Scenario: Adding tag to multi-type item
  Given Edit Item Popup is opened for multi-type item
  When user adds a new tag
  And user clicks on Apply changes button
  Then that item should have that tag displayed in item store

Scenario: Adding special character tag to single-type item
  Given Edit Item Popup is opened for single-type item with tags "t1"
  When user adds tag "@#$%"
  And user clicks on Apply changes button
  Then that item's tags should be "t1, @#$%"

最佳实践:分层编写,兼顾沟通与测试

把两种写法结合起来,让BDD同时承担业务沟通工具和测试验证用例的角色:

  • 顶层用抽象场景作为需求规格:面向产品、开发、测试所有角色,清晰说明核心业务行为,非技术人员也能看懂;
  • 底层用数据驱动的具体场景作为测试用例:覆盖正常流程、边界情况、异常场景,用于自动化执行和严谨验证。

比如用Scenario Outline实现分层:

# 顶层抽象场景:明确核心业务规则
Scenario: Adding tag to item
  Given Edit Item Popup is opened for an item
  When user adds a new tag and applies changes
  Then the tag should be added to that item's tag list

# 底层数据驱动场景:覆盖各类测试情况
Scenario Outline: Adding tag to different item types
  Given Edit Item Popup is opened for <item_type> item with existing tags "<existing_tags>"
  When user adds tag "<new_tag>"
  And user clicks on Apply changes button
  Then the item's tags should be "<expected_tags>"

  Examples:
    | item_type | existing_tags | new_tag  | expected_tags |
    | multi     | t2            | NEWTAG   | t2, NEWTAG    |
    | single    | t1            | SPECIAL! | t1, SPECIAL!  |
    | multi     |               | EMPTY    | EMPTY         |
    | single    | t3            | 超长标签12345678901234567890 | t3, 超长标签12345678901234567890 |

总结:没有绝对的最优写法,核心是根据需求的明确程度调整,让BDD场景既能帮团队对齐业务需求,又能有效支撑测试验证,避免遗漏Bug。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 21:25:21