自動化測試工具Cucumber的簡單介紹,入門篇

背景介紹

隨著

測試

的發展,測試自動化越來越成為人們的關注點。

現在我們公司也在進行介面自動化的推廣,在我看來介面自動化的價值就在於整體專案的迴歸,完成一些沒法透過人力進行的測試,比如

壓力測試

為了解決測試開發人員和

功能測試

人員的同步問題,選擇了Cucumber框架。

Cucumber是一個能夠理解用普通語言描述

測試用例

的行為驅動開發(BDD)的

自動化測試工具

換句話說就是學習成本比較低,並且可以方便測試開發人員和功能測試人員協同合作、開發人員進行公共方法的封裝、功能測試人員進行測試用例的編寫。

自動化測試工具Cucumber的簡單介紹,入門篇

Cucumber組成

由Features、Step_definitions、Cucumber command組成。

Features

·基於Gherkin,支援語言:# language: en (zh-CN)等;

·Features檔案必須以。features命名;

·包含title,多個scenarios,每個scenario包含多個step。

示例如下:多組引數傳參。

```

Features: test //Features關鍵字,測試用例集

Scenario Outline: eating //Scenario Outline關鍵字,測數用例

Given there are cucumbers //Given關鍵字,進行介面請求

When I eat cucumbers //When關鍵字,資料準備

Then I should have cucumbers //Then關鍵字

Examples:

| start | eat | left |

| 12 | 5 | 7 |

| 20 | 5 | 15 |

```

關鍵字詳解:

Feature (功能):test suite (測試用例集)。

Scenario(情景):test case (測試用例)。

Scenario Outline (or Scenario Template):和examples更配。

Given(給定:setup(建立測試所需環境)。

When(當):test(觸發被測事件)。

Then(則):assert(斷言,驗證結果)。

Background(背景):您會發現自己在一個功能的所有場景中重複相同的給定步驟,因為它在每個場景中都是重複的。

這表明這些步驟對於描述場景不是必需的,它們是附帶的細節。您可以透過將這些給定的步驟分組到background部分,將它們移動到後臺。

And(or But):如果你有連續的“給定”、“何時”或“然後”。

“”“(定義多行字串):方便地將較大的文字段傳遞給步驟定義。

|(用來定義表格):資料表便於將值列表傳遞給步驟定義。

Step_definitions

Step定義必須以關鍵字Given、When、Then、And開始,根據正則匹配對應的關鍵字。

根據feature檔案中定義的step編寫對應的測試程式碼。

示例如下:

```java

public class StepDefinition {

private String today;

private String actualAnswer;

@Given(”^today is Sunday$“) //和features中的Given對應

public void today_is_Sunday() {

today = ”Sunday“;

}

@When(”^I ask whether it‘s Friday yet$“) //和features中的When對應

public void i_ask_whether_is_s_Friday_yet() {

actualAnswer = IsItFriday。isItFriday(today);

}

@Then(”^I should be told \“([^\”]*)\“$”) //和features中的Then對應

public void i_should_be_told(String expectedAnswer) {

assertEquals(expectedAnswer, actualAnswer);

}

}

```

Cucumber command

執行*。feature檔案,Cucumber會分析feature檔案中定義的step,然後去step -definitions尋找相匹配的step,執行step中的程式碼。

執行結果以html的形式儲存,fail的情況檢視對應log日誌。

Cucumber開發過程

1。首先使用Cucumber原型Maven外掛建立一個新的專案目錄。

```powershell

mvn archetype:generate -DarchetypeGroupId=io。cucumber -DarchetypeArtifactId=cucumber-archetype -DarchetypeVersion=6。10。4 -DgroupId=hellocucumber -DartifactId=hellocucumber -Dpackage=hellocucumber -Dversion=1。0。0-SNAPSHOT -DinteractiveMode=false

```

專案目錄如下:

自動化測試工具Cucumber的簡單介紹,入門篇

2、在reources資料夾下,建立feature檔案,包括feature、scenarios和step。

```

Feature: Is it Friday yet? //Features關鍵字,測試用例集

Scenario: Sunday isn’t Friday //Scenario Outline關鍵字,測試用例

Given today is Sunday //Given關鍵字,進行介面請求

When I ask whether it‘s Friday yet //When關鍵字,資料準備

Then I should be told “Nope” //Then關鍵字

```

3、在hellocucumber檔案下建立step_definitions。

```java

package hellocucumber;

import io。cucumber。java。en。Given;

import io。cucumber。java。en。Then;

import io。cucumber。java。en。When;

import static org。junit。Assert。*;

class IsItFriday {

static String isItFriday(String today) {

return “Nope”;

}

}

public class StepDefinition {

private String today;

private String actualAnswer;

@Given(“^today is Sunday$”) //和features中的Given對應

public void today_is_Sunday() {

today = “Sunday”;

}

@When(“^I ask whether it’s Friday yet$”) //和features中的When對應

public void i_ask_whether_is_s_Friday_yet() {

actualAnswer = IsItFriday。isItFriday(today);

}

@Then(“^I should be told \”([^\“]*)\”$“) //和features中的Then對應

public void i_should_be_told(String expectedAnswer) {

assertEquals(expectedAnswer, actualAnswer);

}

}

```

4、專案執行,在idea中直接執行hellocucumber資料夾下的Runcucumber。java檔案即可。

```java

import io。cucumber。junit。Cucumber;

import io。cucumber。junit。CucumberOptions;

import org。junit。runner。RunWith;

@RunWith(Cucumber。class)

@CucumberOptions(plugin = {”pretty“})

public class RunCucumberTest {

}

```

測試用例設計

測試用例設計時按介面文件給的標準生成資料,然後填充到如下圖的examples中即可,框架會迴圈進行執行測試用例,生成測試結果。

```

Features: test //Features關鍵字,測試用例集

Scenario Outline: eating //Scenario Outline關鍵字,測試用例

Given there are cucumbers //Given關鍵字,進行介面請求

When I eat cucumbers //When關鍵字,資料準備

Then I should have cucumbers //Then關鍵字

Examples: //Examples關鍵字

| start | eat | left |

| 12 | 5 | 7 |

| 20 | 5 | 15 |

```

後期維護

後續迭代版本功能測試人員和測試開發人員分工進行,功能測試人員維護Features,也就是測試用例。

測試開發人員進行step_definitions的維護,就是一些程式碼邏輯和公共方法,最重要的也就是斷言方法的改動比較大,介面請求就幾乎是格式化的東西。

專案框架定製思路

1。測試前資料準備:類似於登入後獲取請求頭這種在裡面進行實現。

2。測試用例資料:Features檔案中存放。

3。邏輯處理,介面請求:封裝到Step_definitions。

4。公共工具封裝:一些

資料庫

連線,yaml檔案讀取或者一些其他工具的存放地點。

5。框架配置資訊:環境相關資訊放置位置,不同城市、不同資料庫、不同賬號的切換在裡面進行設定。

6。測試報告存放位置:用於測試報告的存放,介面文件的存放。

**文末福利——推薦《自動化測試學習交流群》給大家:

入群方式:

關注+私信 回覆:”頭條“ 就可以免費拿到軟體測試學習資料,同時進入群學習交流~~

自動化測試工具Cucumber的簡單介紹,入門篇