簡介
cucumber是BDD(Behavior-driven development,行為驅(qū)動開發(fā))的一個自動化測試的副產(chǎn)品。它使用自然語言來描述測試,使得非程序員可以理解他們。Gherkin是這種自然語言測試的簡單語法,而Cucumber是可以執(zhí)行它們的工具。關(guān)于BDD有興趣自行了解。附cucumber官網(wǎng)鏈接,里面也有關(guān)于BDD的信息。
cucumber本質(zhì)上是使用根據(jù)正則表達式匹配自然語言,然后依次執(zhí)行對應(yīng)的方法,以達到測試的目的。
本文基本上只是官網(wǎng)的搬運工,摘要了部分信息,最好還是看官網(wǎng)文檔。
Gherkin
Gherkin是自然語言測試的簡單語法。
一個完整的測試是由多個step組成的,step即最小單元,如何復(fù)用step是非常關(guān)鍵的問題。多個step組成一個Scenario,即一個完整的測試case。多個Scenario組成一個Feature,即一組相關(guān)的測試case。
關(guān)鍵字
- Feature
- Example(or Scenario)
- Given,When,Then,And,But(steps)
- Background
- Scenario Outline (or Scenario Template)
- Examples (or Scenarios)
一個簡單的例子
Feature: Is it friday yet?
this is a descriptions
Everybody want to know when it's Friday
Scenario: Sunday isn't Friday
Given today is Sunday
When I ask whether it's Friday yet
Then I should be told "Nope"
Scenario: Friday is Friday
Given today is Friday
When I ask whether it's Friday yet
Then I should be told "TGIF"
Feature
Feature是所有測試的開頭。后面跟一段描述性的文字,表明這個測試文件是干什么的。
description
description是一段擴展性的文字描述,可以跟在Feature、Example、Background、Scenario、Scenario Outline下面。
Example和Scenario
Example和Scenario是一對同義詞,是一個具體的測試case,包含了多個step。一般情況下,都是由Given(給定一個初始條件),When(發(fā)生了什么),Then(結(jié)果是什么)組成的。
Steps
step是cucubmer的最小單元,每個step是由Given, When, Then, And, 或者But開頭的。如果關(guān)鍵詞后面的內(nèi)容是完全一樣的話,那么cucumber會認為這兩句話是重復(fù)的,哪怕前面的關(guān)鍵詞不一樣,如
Given there is money in my account
Then there is money in my account
這種限制也促使我們使用更加準確的語言去描述
Given my account has a balance of £430
Then my account should have a balance of £430
Given
Given一般用于在Scenario中描述系統(tǒng)的初始狀態(tài)。它的目的是使系統(tǒng)在使用前處于一個已只的狀態(tài),要避免在Given中談?wù)摻换ド系氖虑椤?/p>
When
When描述一個事件或者動作。他可以是與系統(tǒng)間的交互,也可以是由另一個系統(tǒng)觸發(fā)的事件。cucumber強烈推薦每個Scenario只有一個When,當你覺得需要加更多的When的時候,通常就是需要拆分成多個Scenario的信號。
Then
Then描述期望的輸出或者結(jié)果。對Then的step definition應(yīng)該使用斷言去比較期望值和實際值,就和單元測試差不多。
But和And
當有幾個Given,When,Then的時候,可以寫成
Example: Multiple Givens
Given one thing
Given another thing
Given yet another thing
When I open my eyes
Then I should see something
Then I shouldn't see something else
也可以用And和But增加其可讀性
Example: Multiple Givens
Given one thing
And another thing
And yet another thing
When I open my eyes
Then I should see something
But 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
- 不要設(shè)置復(fù)雜的狀態(tài),除非該狀態(tài)實際上是客戶需要知道的
- 保持background簡短,因為在閱讀Scenario的時候,是需要記住background的。最好不要超過4行
- 一個feature只能有一個background,如果需要不同的background針對不同的方案,就需要分拆成不同的feature
Scenario Outline
Scenario Outline即Scenario的模板,也可寫作Scenario Template。它可運行相同的Scenario多次。
Scenario: eat 5 out of 12
Given there are 12 cucumbers
When I eat 5 cucumbers
Then I should have 7 cucumbers
Scenario: eat 5 out of 20
Given there are 20 cucumbers
When I eat 5 cucumbers
Then I should have 15 cucumbers
可用Scenario Outline簡化
Scenario Outline: eating
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
DataTable
Gherkin可以傳遞List,Map,稱為DataTable,具體例子查看該github上的文檔
方言
Gherkin支持多種方言,對應(yīng)關(guān)鍵字轉(zhuǎn)換查看該文檔
狀態(tài)共享
不同step里定義的變量,可以通過java類的成員變量達到共享上下文變量的目的。如上例子的today和actualAnswer。
而狀態(tài)泄露會使得scenarios變得脆弱和難以維護。詳細信息查看該文檔
注釋
Gherkin使用#作為注釋符號
step definition
step definition是指通過正則表達式對應(yīng)到一個或多個Gherkin step的java方法。如下例子是上面例子對應(yīng)的step definition
public class FridayYet {
private String today;
private String actualAnswer;
@Given("^today is (.*)$")
public void todayIsSunday(String day) {
this.today = day;
}
@When("^I ask whether it's Friday yet$")
public void iAskWhetherItsFridayYet() {
if (this.today.equals("Friday")) {
this.actualAnswer = "TGIF";
} else {
this.actualAnswer = "Nope";
}
}
@Then("^I should be told \"(.*)\"$")
public void iShouldBeToldNope(String expectedAnswer) {
Assert.assertEquals(expectedAnswer, actualAnswer);
}
}
替代文本
使用/作為替代文本。如下例子可匹配I have {int} cucumber(s) in my belly和I have {int} cucumber(s) in my stomach
I have {int} cucumber(s) in my belly/stomach
3.0.1版本前,無法轉(zhuǎn)義/,始終被解釋為替代文本,3.0.1后用\/轉(zhuǎn)義/
附github版本變更記錄
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() {
}
測試過寫兩個tag,兩個都會生效
結(jié)合Background具體的運行順序是 before tag -> Background -> Scenario -> after tag
- 以上2點是官網(wǎng)提出的,個人認為還有第三點作用:生成report的時候提供分類作用
update at 2019年03月25日
tips: cucumber.api.spring.SpringTransactionHooks(cucmber-spring包)提供了一個spring事務(wù)的鉤子@txn,提供回滾功能。
IDE插件
IDEA Cucumber for Java
report
具體沒看,有興趣可以自行了解
自動化測試之cucumber(四)