沒有箭頭函式
請注意,如果您使用箭頭函式,則必須在步驟之外建立一個代表狀態的變數,以便在步驟之間共享狀態!
let cukesState;
Given('I have {int} cukes in my belly', cukes => {
cukesState = cukes
})
步驟定義是一個帶有表達式的 方法函式區塊函式函式,它將其連結到一個或多個 Gherkin 步驟。當 Cucumber 在情境中執行 Gherkin 步驟時,它將尋找匹配的步驟定義來執行。
為了說明其運作方式,請查看以下 Gherkin 情境
Scenario: Some cukes
Given I have 48 cukes in my belly
步驟的 I have 48 cukes in my belly
部分(Given
關鍵字後面的文字)將與以下步驟定義相符
package com.example;
import io.cucumber.java.en.Given;
public class StepDefinitions {
@Given("I have {int} cukes in my belly")
public void i_have_n_cukes_in_my_belly(int cukes) {
System.out.format("Cukes: %n\n", cukes);
}
}
或者,使用 Java8 lambda
package com.example;
import io.cucumber.java8.En;
public class StepDefinitions implements En {
public StepDefinitions() {
Given("I have {int} cukes in my belly", (Integer cukes) -> {
System.out.format("Cukes: %n\n", cukes);
});
}
}
package com.example
import io.cucumber.java8.En
class StepDefinitions : En {
init {
Given("I have {int} cukes in my belly") { cukes: Int ->
println("Cukes: $cukes")
}
}
}
package com.example
import io.cucumber.scala.{ScalaDsl, EN}
class StepDefinitions extends ScalaDsl with EN {
Given("I have {int} cukes in my belly") { cukes: Int =>
println(s"Cukes: $cukes")
}
}
Given('I have {int} cukes in my belly') do |cukes|
puts "Cukes: #{cukes}"
end
const { Given } = require('cucumber')
Given('I have {int} cukes in my belly', function (cukes) {
console.log(`Cukes: ${cukes}`)
});
步驟定義的表達式可以是正規表達式或 Cucumber 表達式。本節中的範例使用 Cucumber 表達式。如果您偏好使用正規表達式,則每次匹配的捕獲群組 捕獲群組 捕獲群組 輸出參數 輸出參數將作為引數傳遞給步驟定義的 方法函式區塊函式函式。
@Given("I have {int} cukes in my belly")
public void i_have_n_cukes_in_my_belly(int cukes) {
}
Given("I have {int} cukes in my belly") { cukes: Int ->
println("Cukes: $cukes")
}
Given("I have {int} cukes in my belly") { cukes: Int =>
println(s"Cukes: $cukes")
}
Given(/I have {int} cukes in my belly/) do |cukes|
end
Given(/I have {int} cukes in my belly/, function (cukes) {
});
如果捕獲群組 捕獲群組 捕獲群組 輸出參數 輸出參數表達式與註冊的參數類型之一的 regexp
相同,則在將捕獲的字串傳遞給步驟定義的 方法函式區塊函式函式之前,會先轉換該字串。在上面的範例中,cukes
引數將是一個整數,因為內建 int
參數類型的 regexp
為 \d+
。
步驟定義可以透過在實例變數中儲存狀態,將狀態傳輸到後續的步驟定義。
步驟定義不會連結到特定的功能檔案或情境。步驟定義的檔案、類別或套件名稱不會影響它將匹配的 Gherkin 步驟。唯一重要的是步驟定義的表達式。
當 Cucumber 遇到沒有匹配步驟定義的 Gherkin 步驟時,它會列印一個帶有匹配的 Cucumber 表達式的步驟定義片段。您可以使用它作為新步驟定義的起點。
考慮以下 Gherkin 步驟
Given I have 3 red balls
如果您沒有匹配的步驟定義,Cucumber 將建議以下片段
@Given("I have {int} red balls")
public void i_have_red_balls(int int1) {
}
@Given("I have {int} red balls") { balls: Int ->
}
Given("I have {int} red balls") { balls: Int =>
}
Given('I have {int} red balls') do |int1|
end
Given("I have {int} red balls", function (int1) {
});
如果您的未定義步驟的部分與您自己的參數類型相符,建議的片段將會使用它們。如果存在color 參數類型,Cucumber 會在建議的表達式中使用它
@Given("I have {int} {color} balls")
public void i_have_color_balls(int int1, Color color) {
}
@Given("I have {int} {color} balls") { balls: Int, color: Color ->
}
Given("I have {int} {color} balls") { (balls: Int, color: Color) =>
}
Given('I have {int} {color} balls') do |int1, color|
end
Given("I have {int} {color} balls", function (int1, color) {
});
請確保在執行 Cucumber 時使用 summary
外掛程式,以便列印片段。
您可以幫助我們改進此文件。編輯此頁面。