工作原因,最近一直在研究cucumber的 語法以及它和java之間的關系。鑒於是初學者且代碼基礎薄弱,我開始摸索前行,感謝分享博客且也在一路前行的人兒們。
一、cucumber簡介
1、介紹:
cucumber是一種可以使用文本描述語言來執行自動測試用例的工具,使用的語言叫做Gherkin.
Gherkin用於描述軟件的行為而不需要了解具體的實現,使用Gherkin主要有兩個目的文檔和自動測試用例(我們希望能夠和手工測試用例也統一)。Gherkin支持超過40種語言,包括英文、中文。Gherkin可以在任何地方新增注釋,注釋以#開頭,每一個文件都是已.feature結尾,在feature文件中輸入功能描述、場景、步驟,當執行這個功能時每一個步驟都需要編寫ruby代碼塊來實現具體的功能,當前cucumber支持多種語言,除了ruby還可以使用java、javascript來編寫具體定義層的實現。
2、關於Feature
每一個feature文件就是一個模塊功能(如登錄,注冊:login.feature,此處為一個公共方法),Feature之后的描述作為標記整個ticket或者story也或者說是一個項目小模塊的功能概述(如:這是一個增加、編輯功能),Scenario(場景),一個feature中可以有多個Scenario,每個Scenario包含一個到若干個step,步驟使用Given、When、Then、But、And這些關鍵詞進行步驟連接。在執行測試用例時我們一般是從上到下進行執行(如:feature/Given 出錯則一定會給出對應處的提示)
3、Step denfinitions 定義
在項目Feature中,每一個step(given when then)都有一個 step denfinition 來進行方法定義。一般來說我們可以通過自動的方式生成step definition,具體操作你點擊Run查看 log欄下拉可看到報錯信息中含有,然后拷貝好方法到指定的step definition中進行code 編寫即可。
二、cucumber具體實現
1、基本語法為:此處舉例兩種區別一看即知->
1)簡單一點
Scenario
Given
When
Then
2)復雜一點
Scenario
Given
When
And
And
Then
And
3)釋義
Feature:用來描述我們需要測試的模塊,模塊1,2,3...
Scenario: 用來概述功能測試點 如:add/delete
Given:前置條件,比如用戶在哪個頁面進行操作?
When: 描述用戶操作的執行動作,比如click/save
Then: 斷言 表示執行的結果
But-一個步驟中如果存在多個Then操作,第二個開始后面的Then可以用But替代(注意是可以,也可以用Then)
2、寫測試用例范本
1)、Scenario:start new workflow step 1/3 ---一個功能,如登錄,注冊,類似於功能總結為步驟的第一步
Given choose a valid workflow --此處給出一個前提條件,和我們testcase一樣的原理,可以有多個前提條件
When click"Start New" button --此處為動作,動作可以有多個如上第二個語法例子
Then widow prompt:Are you sure you want to start workflow for current responsible?---測試的預期結果,比如現象是什么樣。此處在代碼中可以使用斷言
2)、Scenario:start new workflow step 2/3--一個功能的總結步驟的第二步
Given start new work flow --同上
When choose "Yes" --同上
Then the workflow was started --同上
3)、Scenario:cancle new workflow step 3/3 --一個功能總結步驟的第三步驟
Given start new work flow --同上
When choose "No" --同上
Then cancle the current window--同上
3、Scenario Outline 使用:
If there are many examples, this becomes tedious. We can simplify it with a Scenario Outline:
Scenario Outline: feeding a suckler cow Given the cow weighs <weight> kg When we calculate the feeding requirements Then the energy should be <energy> MJ And the protein should be <protein> kg Examples: | weight | energy | protein | | 450 | 26500 | 215 | | 500 | 29500 | 245 | | 575 | 31500 | 255 | | 600 | 37000 | 305 |
4、background
Occasionally you'll find yourself repeating the same Given steps in all of the scenarios in a feature file. Since it is repeated in every scenario it is an indication that those steps are not essential to describe the scenarios, they are incidental details.
You can literally move such Given steps to the background by grouping them under a Background section before the first scenario:
Background:
大意為:如果有多個案例中存在重復的given and ,可以使用backfround進行替換
Given a $100 microwave was sold on 2015-11-03 And today is 2015-11-18
5、Data Tables
Data Tables are handy for passing a list of values to a step definition:
Given the following users exist: | name | email | twitter | | Aslak | aslak@cucumber.io | @aslak_hellesoy | | Julien | julien@cucumber.io | @jbpros | | Matt | matt@cucumber.io | @mattwynne |
Just like Doc Strings, Data Tables will be passed to the Step Definition as the last argument.
The type of this argument will be DataTable. See the API docs for more details about how to access the rows and cells.
簽名:知識靠的是累積,我只是擔心年紀大了,會忘事兒🙃
