• BDD - Gherkin Language


    引言

    BDD 相关知识,可以参考 BDD 介绍Agile BDD,本文主要介绍 Gherkin 语言,如何用 Gherkin 写 BDD Scnarios,及 Gherkin 的最实践。

    Gherkin Scenarios

    BDD 的核心就是写 Behavior Scenarios 行为场景。最常用的 Scenario 就是一个最简单的 “given-­when-­then”,步骤用直白的英语写,保存在 feature 文件。

    Given some initial state
    When an event occurs
    Then verify an outcome

    Gherkin 是正规化 BDD Scenarios 最常用的语言。大部分 BDD 框架都使用 Gherkin 来写 Scenarios,然而有一些框架实现 Gherkin 会略有不同,但最广泛接受的 Gherkin 标准是由 Cucumber 设立的。正规化的 Scenario 可被转换成自动化测试用例。Gherkin 语法有很多组件,不仅仅是这三个部分 “given­-when­-then”。

    Gherkin Keywords

    下在列出 Gherkin 的关键字,也可参考 Behat Gherkin documentation

    Feature

    • 概述被测的 Product 或 Feature
    • 包括一行标题
    • 包括多行的描述信息
    • 描述信息应该包括 User Story
    • 可以有一个 Background 组成
    • 可以用多个 ScenarioScenario Outline 组成
    • 基于一个 Feature 一个 Feature 文件的原则

    ​Scenario

    • 为一个特定的行为场景 behavior scenario
    • 包括一行标题
    • 包括多个 Given, When, 和 Then 步骤
    • 每个类型的步骤是可选的
    • 步骤顺序很重要

    Given

    • 步骤关键字,定义先决条件(初始状态或语境,环境)
    • 应该将被测 Product 或 Feature 置于期望的状态
    • 可以参数化

    When

    • 步骤关键字用来定义被执行的行为
    • 可参数化

    Then

    • 步骤关键字用来定义由 When 行为执行后期望的结果
    • 可参数化

    ​And

    • 一个追加的步骤用来添加 Given, When, 或 Then
    • 用来代替重复的 Given, When, 或 Then
      例如:Given-Given-When-Then = Given-And-When-Then
    • 与前一个步骤有关联
    • 顺序重要

    ​But

    • 跟 And 关键字功能一样,或许更可读
    • 和 And 可互换

    Background

    • 由 Given 和 And 步骤组成,在每个 Scenario 前执行
    • ​没有标题或秒速
    • 每个 Feature 只能有一个 Background

    Scenario Outline

    • 一个 Scenario 模板
    • 用 “<” and “>” 定义参数名
    • 紧跟 Examples tables 用来提供参数列表的值
    • 可以有一个或多个 Examples tables
    • 当执行用例时,参数会被参数值取代

    ​Examples

    为一个 Scenario Outline 用来提供参数列表的值
    参数表的每行的值表示和一个测试用例的值绑定
    可以有一行或多行参数值

    ​ |

    • 用于 Examples tables and step tables 的分隔符

    ​ @

    • tag 的前缀
    • tag 可以放在 Feature 或 Scenario 区域前
    • tag 用来过滤 Scenarios

    ​​​​​

    注释的前缀
    注释的步骤不会被 Gherkin 解析
    并非所有的 BDD 框架都支持

    Gherkin Scenario Examples

    Basic format

    Feature: Some terse yet descriptive text of what is desired
    Textual description of the business value of this feature
    Business rules that govern the scope of the feature
    Any additional information that will make the feature easier to understand
    Put the user story here

    Scenario: Some determinable business situation
    Given some precondition
    And some other precondition
    When some action by the actor with parameter value “1234”
    And some other action
    And yet another action
    Then some testable outcome is achieved
    And something else we can check happens too

    Scenario: A different situation
    …​

    ​Simple Scenario

    Scenario: Simple Google search
    Given a web browser is on the Google page
    When the search phrase “panda” is entered
    Then results for “panda” are shown​

    ​Scenario with “And” and “But”

    Scenario: Simple Google search with related results
    Given a web browser is on the Google page
    When the search phrase “panda” is entered
    Then results for “panda” are shown
    And the related results include “Panda Express”
    But the related results do not include "pandemonium"​

    ​Feature with Background Section

    Feature: Simple Google Search
    As a web surfer,
    I want to search Google,
    So that I can learn new things.

    Background:
    Given a web browser is on the Google page

    Scenario: Simple Google search for pandas
    When the search phrase “panda” is entered
    Then results for “panda” are shown

    Scenario: Simple Google search for elephants
    When the search phrase “elephant” is entered
    Then results for “elephant” are shown​

    ​Scenario Outline

    Scenario Outline: Simple Google Searches
    Given a web browser is on the Google page
    When the search phrase “” is entered
    Then results for “” are shown
    And the related results include “”

    Examples: Animals
    | phrase | related |
    | panda | Panda Express |
    | elephant | Elephant Man |

    Examples: Planets
    | phrase | related |​​​​
    | Mercury | Mercury Cars |
    | Saturn | Saturn Rings |​

    Step Table

    #Step 中的 table 会传到对应的 step definitions
    #任何 step 都可以带个 table 参数

    Scenario: Google search with multiple related results
    Given a web browser is on the Google page
    When the search phrase “panda” is entered
    Then results for “panda” are shown
    And the following related results are shown
    | related |
    | Panda Express |
    | giant panda |
    | panda videos |​

    Scenarios with Tags

    @wip
    Feature: Simple Google Search
    As a web surfer,
    I want to search Google,
    So that I can learn new things.

    @automated @panda
    Scenario: Simple Google search for pandas
    Given a web browser is on the Google page
    When the search phrase “panda” is entered
    Then results for “panda” are shown

    @manual @elephant
    Scenario: Simple Google search for elephants
    Given a web browser is on the Google page
    When the search phrase “elephant” is entered
    Then results for “elephant” are shown​

    Best Practices

    尽管 Gherkin 语法非常简答,但是写一个好的 Scenarios 是有挑战的。下面列出一些建议有助于写好的 Scenarios,对测试人员非常重要。

    1. Scenarios 要保持简明扼要,阅读起来更像英语而不是代码
    2. 不同的 Feature 用不同的 Feature 文件分开
    3. 在 Feature 描述部分,包含 user story 信息 (“As a ___, I want ___, so that __”)
    4. Scenarios 标题要合理,当用例执行失败,能显而易见什么问题出错
    5. 用第三人称写 Scenarios steps,避免用第一人称 “I/me/we/us” 和 “you”
    6. 在 step definitions 里,参数值要用双引号
    7. 尽可能重用 steps,这样有助于加快测试进度
    8. 参数泛型化,提高 steps 的重用
    9. 用 scenario outlines 覆盖测试数据组合
    10. 限制 table 参数的列数,超过 4 列变得不好管理
    11. 对 scenario outlines,设计唯一的数据组合,避免测试用例重复
    12. 提倡更通用的,高级别的 steps,而不是特定的,低级别的 steps
    13. Scenarios 应该是叙述一个故事,而 step definitions 应该编码实现细节
    14. 通常一个 Scenario,不超过 10 个 steps,一般 3-6 个steps 最合适。
    15. 如果一个 Scenario 有太多步骤,尝试下列方法:
      识别覆盖的行为,试着将他们分开到不同的 Scenarios
      合并多条 steps 成一个总的详情,更多细节在 step definition 用代码实现。
      简化 Scenarios 的设定
      判断 Scenario 是否适合 BDD
    16. 写 Scenarios 时,PO,Dev,QA 应该协作
    17. 有必要加一些注释来说明一些额外信息
    18. 避免 Feature 文件过长
    19. step definitions 最好按字母排序

    针对 Scenarios title
    每个 Scenario 应该专注于一个单一的行为
    反复阅读,
    改进措辞
    每次访问,重新审议名字

  • 相关阅读:
    Pyside6:信号与槽
    Spring04
    Java后端架构技术面试汇总:基础+设计模式+MySQL+分布式+微服务等
    Python Opencv实践 - LBP特征提取
    使用LangChain与chatGPT API开发故事推理游戏-海龟汤
    造车先做三蹦子-之三:自制数据集(6x6数据集)230103
    <人生重开模拟器>——《Python项目实战》
    SSM+基于web的《计算机基础》自学系统的设计与开发 毕业设计-附源码221509
    TensorFlow入门(十八、激活函数)
    软件测试面试题:请试着比较一下黑盒测试、白盒测试、单元测试、集成测试、系统测试、验收测试的区别与联系?
  • 原文地址:https://blog.csdn.net/wumingxiaoyao/article/details/126440051