BDD 相关知识,可以参考 BDD 介绍 和 Agile BDD,本文主要介绍 Gherkin 语言,如何用 Gherkin 写 BDD Scnarios,及 Gherkin 的最实践。
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 的关键字,也可参考 Behat Gherkin documentation
Background 组成Scenario 和 Scenario Outline 组成为一个 Scenario Outline 用来提供参数列表的值
参数表的每行的值表示和一个测试用例的值绑定
可以有一行或多行参数值
注释的前缀
注释的步骤不会被 Gherkin 解析
并非所有的 BDD 框架都支持
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
…
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: 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: 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: 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 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 |
@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
尽管 Gherkin 语法非常简答,但是写一个好的 Scenarios 是有挑战的。下面列出一些建议有助于写好的 Scenarios,对测试人员非常重要。
针对 Scenarios title
每个 Scenario 应该专注于一个单一的行为
反复阅读,
改进措辞
每次访问,重新审议名字