SpecFlow 笔记 第一弹 (Gherkin)
Behavior Driven Development .BDD,Specflow依照BDD的指导思想而形成的一种实践工具。Specflow源于Cucumber,概念和用途几乎一样,可以理解为Specflow是.net平台下的Cucumber。几乎可以用自然语言编写测试用例。Gherkin是这种自然语言测试的简单语法,而Cucumber是可以执行它们的工具。
Gherkin
这是一种业务人员可读懂的,特定领域的语言,它可以让你描述软件的行为,而不用详细说明如何实现这个行为。有几个约定:
- 一个gherkin源文件包含单个功能的说明。
- 源文件具有扩展名* .feature。
- 每个gherkin场景都有一个基本的模式,其中包括:(假如),事件(当)和结果(那么)
一个完整的测试是由多个step组成的,step即最小单元,如何复用step是非常关键的问题。多个step组成一个Scenario,即一个完整的测试case。多个Scenario组成一个Feature,即一组相关的测试case。
关键字
- Feature
- Example(or Scenario)
- Given,When,Then,And,But(steps)
- Background
- Scenario Outline (or Scenario Template)
- Examples (or Scenarios)
Example
Feature: SmokeTestingIn order to avoid silly mistakesAs a math idiotI want to be told the sum of two numbers@mytag
Scenario: Add two numbersGiven the first number is 50And the second number is 70When the two numbers are addedThen the result should be 120
Feature
Feature是所有测试的开头。后面跟一段描述性的文字,表明这个测试文件是干什么的。
description
description是一段扩展性的文字描述,可以跟在Feature、Example、Background、Scenario、Scenario Outline下面。
Example和Scenario
Example和Scenario是一对同义词,是一个具体的测试case,包含了多个step。一般情况下,都是由Given(给定一个初始条件),When(发生了什么),Then(结果是什么)组成的。
Steps
step是cucubmer的最小单元,每个step是由Given, When, Then, And, 或者But开头的。如果关键词后面的内容是完全一样的话,那么cucumber会认为这两句话是重复的,哪怕前面的关键词不一样,如
Given the first number is 50
Then the first number is 50
这种限制也促使我们使用更加准确的语言去描述
Given the first number is 50
Then the first number of the XXX is 50
Given
Given一般用于在Scenario中描述系统的初始状态。它的目的是使系统在使用前处于一个已只的状态,要避免在Given中谈论交互上的事情。
When
When描述一个事件或者动作。他可以是与系统间的交互,也可以是由另一个系统触发的事件。cucumber强烈推荐每个Scenario只有一个When,当你觉得需要加更多的When的时候,通常就是需要拆分成多个Scenario的信号。
Then
Then描述期望的输出或者结果。对Then的step definition应该使用断言去比较期望值和实际值,就和单元测试差不多。
But和And
可以有多个 Given,When,Then的时候,可以写成
Example: Multiple GivensGiven one thingGiven another thingGiven yet another thingWhen I open my eyesThen I should see somethingThen I shouldn't see something else
也可以用And和But增加其可读性
Example: Multiple GivensGiven one thingAnd another thingAnd yet another thingWhen I open my eyesThen I should see somethingBut I shouldn't see something else
step definition里个人并不建议使用@And和@But,只使用@Given,@When,@Then,这样语言更加明确,因为And和But都可以在Given,When,Then的下面使用。
Background
当在同一个Feature里有多个Scenario有相同Given的时候,可以使用Background将这些Given抽到一起。这样这个Feature的每个Scenario在运行的时候,都会先运行一次Background。每个Feature里只能有一个Background。
tips
- 不要设置复杂的状态,除非该状态实际上是客户需要知道的
- 保持background简短,因为在阅读Scenario的时候,是需要记住background的。最好不要超过4行
- 一个feature只能有一个background,如果需要不同的background针对不同的方案,就需要分拆成不同的feature
- 随机执行,否则需要加上序号
@mytag Scenario: 01 Add two numbersGiven the first number is 50And the second number is 70When the two numbers are addedThen the result should be 120Scenario: 02 Add two numbersGiven the first number is 50And the second number is 70When the two numbers are addedThen the result should be 120
Scenario Outline
Scenario Outline即Scenario的模板,也可写作Scenario Template。它可运行相同的Scenario多次。
Scenario: eat 5 out of 12Given there are 12 cucumbersWhen I eat 5 cucumbersThen I should have 7 cucumbersScenario: eat 5 out of 20Given there are 20 cucumbersWhen I eat 5 cucumbersThen I should have 15 cucumbers
可用Scenario Outline简化
Scenario Outline: eatingGiven there are cucumbersWhen I eat cucumbersThen I should have cucumbersExamples:| start | eat | left || 12 | 5 | 7 || 20 | 5 | 15 |
DataTable
Gherkin可以传递List,Map,称为DataTable,https://github.com/cucumber/cucumber/tree/master/datatable
方言
Gherkin支持多种方言,
下面是一个由挪威语编写的Gherkin场景:
# language: no
Funksjonalitet: Gjett et ordEksempel: Ordmaker starter et spillNår Ordmaker starter et spillSå må Ordmaker vente på at Gjetter blir medEksempel: Gjetter blir medGitt at Ordmaker har startet et spill med ordet "bløtt"Når Gjetter blir med på Ordmakers spillSå må Gjetter gjette et ord på 5 bokstaver
特性文件第一行的# language: header告诉Cucumber使用什么口语—例如# language: fr表示法语。如果省略此标题,Cucumber将默认为English (en)。
一些Cucumber实现还允许在配置中设置默认语言,因此不需要在每个文件中都放置# language头。
状态共享
不同step里定义的变量,可以通过java类的成员变量达到共享上下文变量的目的。如上例子的today和actualAnswer。
而状态泄露会使得scenarios变得脆弱和难以维护。https://docs.cucumber.io/cucumber/state/
注释
Gherkin使用#作为注释符号
tag
tag提供2个作用
- 提供@before和@after的钩子(tagged-hooks)
- 运行时只运行指定tag的用例
tag具有继承特性,即在Feature上标记tag,该Feature下的Scenario,step会继承该tag。
@simpleDemo
Feature: Is it friday yet?
@RunWith(Cucumber.class)
@CucumberOptions(tags = "@simpleDemo")// 指定只运行代用@simpleDemo的
public class CucumberTest {
}
@Before("@simpleDemo")
public void beforeOperation() {}@After("@simpleDemo")
public void afterOperation() {}
结合Background具体的运行顺序是 before tag -> Background -> Scenario -> after tag
参照链接:
https://www.jianshu.com/p/3857f2c3a8d4
https://www.jianshu.com/p/43cb0e79f075
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
