避免箭頭函式
ES6 箭頭函式 () => {}
會將 this
綁定到目前的上下文,這會阻止鉤子和步驟定義之間共享狀態。
Cucumber 可用於根據 Gherkin 功能文件中描述的案例來實作自動化測試。
在步驟定義中給出的範例中,Cucumber 從步驟中提取文字 48
,將其轉換為 int
,並將其作為參數傳遞給 方法函式區塊函式函式。
方法函式區塊函式函式中的參數數量必須與表達式中 擷取群組 擷取群組 擷取群組 輸出參數 輸出參數 的數量相符。(如果存在不匹配,Cucumber 將會拋出錯誤)。
可以透過在步驟定義中使用 DataTable 物件作為最後一個參數來存取 Gherkin 中的資料表。此轉換可以由 Cucumber 或手動完成。
根據表格形狀,作為下列其中一個集合:根據表格形狀,作為下列其中一個集合:List<List<String>> table
List<Map<String, String>> table
Map<String, String> table
Map<String, List<String>> table
Map<String, Map<String, String>> table
將 List<String>
List<String>
java.util.List[String]
字串列表字串列表傳遞給步驟定義最簡單的方法是使用資料表
Given the following animals:
| cow |
| horse |
| sheep |
將參數宣告為 List<String>
List<String>
java.util.List[String]
字串列表字串列表,但不要在表達式中定義任何 擷取群組 擷取群組 擷取群組 輸出參數 輸出參數
註解方法樣式
@Given("the following animals:")
public void the_following_animals(List<String> animals) {
}
註解方法樣式
@Given("the following animals:")
fun the_following_animals(animals: List<String>) {
}
Given("the following animals:") { animals: java.util.List[String] =>
}
在這種情況下,DataTable
會在 Cucumber 呼叫步驟定義之前,自動由 Cucumber (使用 DataTable.asList(String.class)
) 扁平化為 List<String>
List<String>
java.util.List[String]
字串陣列字串陣列。
注意:除了字串、整數、浮點數、BigInteger 和 BigDecimal 的集合之外,也支援 Byte、Short、Long 和 Double。
注意:除了字串、整數、浮點數、BigInteger 和 BigDecimal 的集合之外,也支援 Byte、Short、Long 和 Double。
注意:目前,Cucumber Scala 不支援使用 Scala 集合類型。
請參閱 Github
此外,請參閱 cucumber-jvm data-tables
此外,請參閱 cucumber-jvm data-tables
步驟類似於方法呼叫或函式調用。
例如
Given I have 93 cucumbers in my belly
在此步驟中,您正在「呼叫」具有一個參數的上述步驟定義:值 93
。
步驟在您的 features/\*.feature
*.feature
*.feature
*.feature
*.feature
檔案中宣告。
Regexp
進行比對回想一下,步驟定義以介系詞或副詞 (Given
、When
、Then
、And
、But
) 開頭。
所有步驟定義都會在 Cucumber 開始執行功能檔案中的純文字之前載入(和定義)。
一旦執行開始,對於每個步驟,Cucumber 都會尋找具有相符 Regexp
的已註冊步驟定義。如果找到,它將會執行它,並將 Regexp
中的所有 擷取群組 擷取群組 擷取群組 輸出參數 輸出參數 和變數作為參數傳遞給 方法函式區塊函式函式。
當 Cucumber 註冊或尋找步驟定義時,所使用的特定介系詞/副詞沒有意義。
此外,請查看多行步驟參數,以取得更多關於如何將整個表格或較大的字串傳遞給步驟定義的資訊。
每個步驟可以有下列其中一個結果
當 Cucumber 找到相符的步驟定義時,它將會執行它。如果步驟定義中的區塊沒有引發錯誤,則該步驟會標示為成功(綠色)。您從步驟定義中 return
的任何內容都無關緊要。
當 Cucumber 找不到相符的步驟定義時,該步驟會標示為未定義(黃色),並且會跳過情節中的所有後續步驟。
當步驟定義的 方法函式程式碼區塊函式函式 呼叫 pending
方法函式程式碼區塊函式函式 時,該步驟會被標記為 pending(黃色,如同 undefined
的步驟),表示您還有工作要做。
當執行步驟定義的 方法函式程式碼區塊函式函式 時,如果發生錯誤,該步驟會被標記為失敗(紅色)。從步驟定義中回傳的任何值都無關緊要。
回傳 nil
null
null
null
null
或 false
並不會導致步驟定義失敗。
在 undefined
、pending
或 failed
的步驟之後的步驟永遠不會被執行,即使有相符的步驟定義也是如此。這些步驟會被標記為已跳過(青色)。
步驟定義必須是唯一的,Cucumber 才能知道要執行什麼。如果您使用不明確的步驟定義,Cucumber 會引發 Cucumber::Ambiguous
錯誤, Cucumber 會引發 AmbiguousStepDefinitionsException
,Cucumber 會引發 AmbiguousStepDefinitionsException
,Cucumber 會引發 AmbiguousStepDefinitionsException
, 該步驟/情境會得到「Ambiguous」的結果, 告訴您要修正這個不明確之處。
鉤子是在 Cucumber 執行週期中的不同時間點可以執行的程式碼區塊。它們通常用於在每個情境之前和之後設定和拆解環境。
鉤子的定義位置不會影響它為哪些情境或步驟執行。如果您想要更精細的控制,可以使用條件式鉤子。
您可以在任何類別中宣告鉤子。
您可以在任何類別中宣告鉤子。
您可以在任何類別、特徵或物件中宣告鉤子。
您可以在 features/support/env.rb
檔案或 features/support
目錄下的任何其他檔案中宣告鉤子(例如,在名為 support/hooks.rb
的檔案中)。
您可以在 features/support/env.js
檔案或 features/support
目錄下的任何其他檔案中宣告鉤子(例如,在名為 features/support/hooks.js
的檔案中)。
情境鉤子會在每個情境執行。
Before
鉤子會在每個情境的第一個步驟之前執行。
註解方法樣式
@Before
public void doSomethingBefore() {
}
Lambda 風格
Before(() -> {
});
Lambda 風格
Before { scenario: Scenario ->
// doSomething
}
Before { scenario: Scenario =>
// doSomething
}
// Import the Before function
const { Before } = require('@cucumber/cucumber')
Before(async function () {
})
Before do
# Do something before each scenario
end
如果需要,您可以為鉤子指定明確的順序。
如果需要,您可以為鉤子指定明確的順序。
如果需要,您可以為鉤子指定明確的順序。
註解方法樣式
@Before(order = 10)
public void doSomething(){
// Do something before each scenario
}
Lambda 風格
Before(10, () -> {
// Do something before each scenario
});
Before(10) { scenario: Scenario ->
// Do something before each scenario
}
package io.cucumber.example
import io.cucumber.java.AfterAll
import io.cucumber.java.BeforeAll
@BeforeAll
fun beforeAll() {
println("before all")
}
@AfterAll
fun afterAll() {
println("after all")
}
Before(10) { scenario: Scenario =>
// Do something before each scenario
}
Before
鉤子會以宣告的相同順序執行。
Before
鉤子會以宣告的相同順序執行。
After
鉤子會在每個情境的最後一個步驟之後執行,即使步驟結果為 failed
、undefined
、pending
或 skipped
也是如此。
註解方法樣式
@After
public void doSomethingAfter(Scenario scenario){
// Do something after after scenario
}
Lambda 風格
After((Scenario scenario) -> {
});
Lambda 風格
After { scenario: Scenario ->
// doSomething
}
After { scenario: Scenario =>
// doSomething
}
After(async function (scenario) {
})
After do |scenario|
end
scenario
參數是選用的。如果您使用它,您可以檢查情境的狀態。
例如,您可以使用 WebDriverWebDriverWebDriverWebDriverCapybara 為失敗的情境拍攝螢幕截圖,並將它們嵌入 Cucumber 的報告中。
請參閱瀏覽器自動化頁面,以取得如何執行此操作的範例。
以下是一個範例,其中我們會在第一次失敗時退出(這在某些情況下可能很有用,例如持續整合,其中快速回饋非常重要)。
After do |s|
# Tell Cucumber to quit after this scenario is done - if it failed.
Cucumber.wants_to_quit = true if s.failed?
end
Around
鉤子會在情境「周圍」執行。這可以用於將情境的執行包裝在區塊中。Around
鉤子會接收一個 Scenario
物件和一個區塊 (Proc
) 物件。當您呼叫 block.call
時,將會執行情境。
以下範例會導致標記為 @fast
的情境,如果執行時間超過 0.5 秒就會失敗。
Around('@fast') do |scenario, block|
Timeout.timeout(0.5) do
block.call
end
end
Around
鉤子。Around
鉤子。Around
鉤子。Around
鉤子。步驟鉤子會在步驟之前和之後被呼叫。鉤子具有「環繞呼叫」語意,這表示如果執行 BeforeStep
鉤子,則無論步驟的結果如何,也會執行 AfterStep
鉤子。如果步驟未通過,則會跳過後續的步驟及其鉤子。
步驟鉤子會在步驟之前和之後被呼叫。鉤子具有「環繞呼叫」語意,這表示如果執行 BeforeStep
鉤子,則無論步驟的結果如何,也會執行 AfterStep
鉤子。如果步驟未通過,則會跳過後續的步驟及其鉤子。
步驟鉤子會在步驟之前和之後被呼叫。鉤子具有「環繞呼叫」語意,這表示如果執行 BeforeStep
鉤子,則無論步驟的結果如何,也會執行 AfterStep
鉤子。如果步驟未通過,則會跳過後續的步驟及其鉤子。
步驟鉤子會在步驟之前和之後被呼叫。鉤子具有「環繞呼叫」語意,這表示如果執行 BeforeStep
鉤子,則無論步驟的結果如何,也會執行 AfterStep
鉤子。如果步驟未通過,則會跳過後續的步驟及其鉤子。
BeforeStep
鉤子。
註解方法樣式
@BeforeStep
public void doSomethingBeforeStep(Scenario scenario){
}
Lambda 風格
BeforeStep((Scenario scenario) -> {
});
Lambda 風格
BeforeStep { scenario: Scenario ->
// doSomething
}
BeforeStep { scenario: Scenario =>
// doSomething
}
BeforeStep(async function({pickle, pickleStep, gherkinDocument, testCaseStartedId, testStepId}) {
// doSomething
})
BeforeStep({tags: "@foo"}, async function() {
// apply this hook to only specific scenarios
})
AfterStep do |scenario|
end
註解方法樣式
@AfterStep
public void doSomethingAfterStep(Scenario scenario){
}
Lambda 風格
AfterStep((Scenario scenario) -> {
});
Lambda 風格
AfterStep { scenario: Scenario ->
// doSomething
}
AfterStep { scenario: Scenario =>
// doSomething
}
AfterStep(async function({pickle, pickleStep, gherkinDocument, result, testCaseStartedId, testStepId}) {
// doSomething
})
可以根據情境的標籤有條件地選擇執行鉤子。若要僅針對某些情境執行特定的鉤子,您可以將 Before
或 After
Before
或 After
Before
或 After
Before
或 After
Before
、After
、Around
或 AfterStep
鉤子與標籤運算式建立關聯。
註解方法樣式
@After("@browser and not @headless")
public void doSomethingAfter(Scenario scenario){
}
Lambda 風格
After("@browser and not @headless", (Scenario scenario) -> {
});
Lambda 風格
After (arrayOf("@browser and not @headless")) { scenario: Scenario ->
driver.quit()
}
After("@browser and not @headless") { scenario: Scenario =>
}
Before({tags: '@browser and not @headless'}, async function () {
})
Before('@browser and not @headless') do
end
請參閱關於標籤的更多文件。
全域鉤子會在任何情境執行之前執行一次,或在所有情境都執行完畢之後執行一次。
將程式碼放在 env.rb
檔案(或 features/support
目錄下的任何其他檔案)的頂層。
BeforeAll
會在執行任何情境之前執行。
BeforeAll do
# Do something before any scenario is executed
end
const { BeforeAll } = require('@cucumber/cucumber');
BeforeAll(async function () {
// perform some shared setup
});
註解方法樣式
@BeforeAll
public static void beforeAll() {
// Runs before all scenarios
}
BeforeAll {
// doSomething
}
BeforeAll {
// doSomething
}
AfterAll
會在執行完畢所有情境之後執行。
AfterAll do
# Do something after all scenarios have been executed
end
const { AfterAll } = require('@cucumber/cucumber');
AfterAll(async function () {
// perform some shared setup
});
註解方法樣式
@AfterAll
public static void afterAll() {
// Runs after all scenarios
}
AfterAll {
// doSomething
}
AfterAll {
// doSomething
}
您也可以提供一個 InstallPlugin
鉤子,該鉤子會在 Cucumber 設定完成之後執行。您提供的區塊將會傳遞給 Cucumber 的組態(Cucumber::Cli::Configuration
的一個實例),以及一個作為註冊表的 Cucumber 內部元件包裝器。
InstallPlugin do |config, registry|
puts "Features dwell in #{config.feature_dirs}"
end
這個鉤子只會執行一次:在載入支援之後,以及在載入任何 feature 之前。
您可以使用這個鉤子來擴充 Cucumber。例如,您可以影響如何載入 feature,或以程式設計方式註冊自訂格式器。
cucumber-wire 是一個如何使用 InstallPlugin 以及 Cucumber 外掛程式可以做什麼的好例子。
Cucumber-JVM 不支援 InstallPlugin
鉤子。Cucumber-JVM 不支援 InstallPlugin
鉤子。Cucumber-JVM 不支援 InstallPlugin
鉤子。 Cucumber.js 不支援 InstallPlugin
鉤子。
AfterConfiguration
已被棄用,改用 BeforeAll
和 InstallPlugin
,具體取決於您的需求。
標籤是組織您的 feature 和情境的好方法。
它們可用於兩個目的
請考慮以下範例
@billing
Feature: Verify billing
@important
Scenario: Missing product description
Given hello
Scenario: Several products
Given hello
一個 feature 或情境可以擁有任意多個標籤。用空格分隔它們
@billing @bicker @annoy
Feature: Verify billing
標籤可以放在以下 Gherkin 元素上方
Feature
情境
情境大綱
範例
在 Scenario Outline
中,您可以在不同的範例中使用標籤,如下所示
Scenario Outline: Steps will run conditionally if tagged
Given user is logged in
When user clicks <link>
Then user will be logged out
@mobile
Examples:
| link |
| logout link on mobile |
@desktop
Examples:
| link |
| logout link on desktop |
不可能將標籤放在 Background
或步驟(Given
、When
、Then
、And
和 But
)上方。
標籤由子元素繼承。
位於 Feature
上方的標籤將會被 Scenario
、Scenario Outline
或 Examples
繼承。
位於 Scenario Outline
上方的標籤將會被 Examples
繼承。
您可以告訴 Cucumber 只執行具有特定標籤的情境
對於 JUnit 5,請參閱 cucumber-junit-platform-engine 文件
對於 JUnit 4 和 TestNG,請使用 JVM 系統屬性
mvn test -Dcucumber.filter.tags="@smoke and @fast"
或環境變數
# Linux / OS X:
CUCUMBER_FILTER_TAGS="@smoke and @fast" mvn test
# Windows:
set CUCUMBER_FILTER_TAGS="@smoke and @fast"
mvn test
或變更您的 JUnit 4/TestNG 執行器類別
對於 JUnit 5,請參閱 cucumber-junit-platform-engine 文件
對於 JUnit 4 和 TestNG,請使用 JVM 系統屬性
mvn test -Dcucumber.filter.tags="@smoke and @fast"
或環境變數
# Linux / OS X:
CUCUMBER_FILTER_TAGS="@smoke and @fast" mvn test
# Windows:
set CUCUMBER_FILTER_TAGS="@smoke and @fast"
mvn test
或變更您的 JUnit 4/TestNG 執行器類別
對於 JUnit 5,請參閱 cucumber-junit-platform-engine 文件
對於 JUnit 4 和 TestNG,請使用 JVM 系統屬性
mvn test -Dcucumber.filter.tags="@smoke and @fast"
或環境變數
# Linux / OS X:
CUCUMBER_FILTER_TAGS="@smoke and @fast" mvn test
# Windows:
set CUCUMBER_FILTER_TAGS="@smoke and @fast"
mvn test
或變更您的 JUnit 4/TestNG 執行器類別
@CucumberOptions(tags = "@smoke and @fast")
public class RunCucumberTest {}
@CucumberOptions(tags = "@smoke and @fast")
class RunCucumberTest
@CucumberOptions(tags = "@smoke and @fast")
class RunCucumberTest {}
# You can omit the quotes if the expression is a single tag
./node_modules/.bin/cucumber.js --tags "@smoke and @fast"
# You can omit the quotes if the expression is a single tag
cucumber --tags "@smoke and @fast"
您可以告訴 Cucumber 忽略具有特定標籤的情境
使用 JUnit 執行器類別
使用 JUnit 執行器類別
使用 JUnit 執行器類別
@CucumberOptions(tags = "not @smoke")
public class RunCucumberTest {}
@CucumberOptions(tags = "not @smoke")
class RunCucumberTest
@CucumberOptions(tags = "not @smoke")
class RunCucumberTest {}
# You can omit the quotes if the expression is a single tag
./node_modules/.bin/cucumber.js --tags "not @smoke"
# You can omit the quotes if the expression is a single tag
cucumber --tags "not @smoke"
標籤表達式是一種中綴布林表達式。以下是一些範例
表達式 | 描述 |
---|---|
@fast |
標記為 @fast 的情境 |
@wip and not @slow |
標記為 @wip 且未同時標記為 @slow 的情境 |
@smoke and @fast |
同時標記為 @smoke 和 @fast 的情境 |
@gui or @database |
標記為 @gui 或 @database 的情境 |
對於更進階的標籤表達式,您可以使用括號來提高清晰度,或變更運算符優先順序
(@smoke or @ui) and (not @slow)
在將標籤用於文件編寫時,唯一限制您的是您的想像力。
標籤可以參考外部系統中的 ID,例如需求管理工具、問題追蹤器或測試管理工具
@BJ-x98.77 @BJ-z12.33
Feature: Convert transaction
您可以使用自訂 Cucumber 報告外掛程式,將標籤轉換為指向外部工具中文件的連結。
使用標籤的另一種創新方式是追蹤特定功能在開發流程中的位置
@qa_ready
Feature: Index projects
在分散式環境中,Cucumber-Rails 建置了一個 Rake 任務,可以識別 @wip
標籤。但是,任何字串都可以用作標籤,並且任何情境或整個功能都可以有多個相關聯的標籤。
分散式 config/cucumber.yml
中包含的預設設定檔包含以下幾行
<%
. . .
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} --strict --tags ~@wip"
%>
default: <%= std_opts %> features
. . .
請注意尾部的選項 --tags ~@wip
。Cucumber 允許透過在 --tags
參數前加上波浪符號 (~
) 來否定標籤。這會告訴 Cucumber 不要處理具有此標籤的功能和情境。如果您未指定不同的設定檔 (cucumber -p profilename
),則會使用預設設定檔。如果使用預設設定檔,則 --tags ~@wip
會導致 Cucumber 跳過任何具有此標籤的情境。這將覆蓋命令列中傳遞的 --tags=@authen
選項,因此您將看到以下情況
$ cucumber --tags=@authent
Using the default profile...
0 scenarios
0 steps
0m0.000s
自 0.6.0 版起,無法再透過在命令列上的 Cucumber 參數清單中加入 --tags=@wip
來克服此預設設定,因為現在所有 --tags
選項都已 AND 在一起。因此,--tags @wip
AND --tags ~@wip
的組合在任何地方都會失敗。
您必須在 config/cucumber.yml
中建立特殊的設定檔來處理此問題,或修改預設設定檔以符合您的需求。
@wip
標籤是一種特殊情況。如果任何標記為 @wip
的情境在沒有錯誤的情況下通過所有步驟,並且也傳遞了 --wip
選項,則 Cucumber 會回報執行失敗(因為標記為正在進行中的情境不應該通過!)
另請注意,--strict
和 --wip
選項是互斥的。
您可以透過在傳遞給 --tags
選項的標籤名稱結尾加上冒號和數字來控制功能中特定標籤的出現次數,如下所示
$ cucumber --tags=@wip:3 features/log\*
如果在特定的 Cucumber 執行期間執行的所有功能中,該標籤的出現次數超過指定次數,則會產生警告訊息。如果同時傳遞了 --strict
選項(就像預設設定檔的情況一樣),則執行將會失敗,而不是產生警告。
限制出現次數通常與 @wip
標籤結合使用,以將未指定情境的數量限制在可管理的範圍內。遵循 看板 或 精實軟體開發 方法的人會發現這很有用。
Cucumber 是一個 Java 程式庫,具有適用於不同工具和平台的擴充功能。它透過執行 JUnit 4、JUnit 5、您的建置工具、您的 IDE 或 CLI 來啟動。
Cucumber 是一個 Java 程式庫,具有適用於不同工具和平台的擴充功能。它透過執行 JUnit 4、JUnit 5、您的建置工具、您的 IDE 或 CLI 來啟動。
Cucumber 是一個 Java 程式庫,具有適用於不同工具和平台的擴充功能。它透過執行 JUnit 4、JUnit 5、您的建置工具、您的 IDE 或 CLI 來啟動。
Cucumber 是一個命令列工具。它透過從命令列或建置腳本執行 cucumber-js
來啟動。
Cucumber 是一個命令列工具。它透過從命令列或建置腳本執行 cucumber
來啟動。
可以設定 Cucumber 應如何執行功能。
最常見的選項是從命令列執行 Cucumber。預設情況下,Cucumber 會將根程式庫目錄下任何以 .rb
結尾的內容視為步驟定義檔案。因此,位於 features/models/entities/step_definitions/anything.rb
中的步驟可以在 features/views/entity_new
中包含的功能檔案中使用,前提是
./features
) 上調用的;或者最常見的選項是從命令列執行 Cucumber。預設情況下,Cucumber 會將根目錄下任何以 .js
結尾的內容視為步驟定義檔案。因此,位於 features/models/entities/step-definitions/anything.js
中的步驟可以在功能檔案中使用,前提是
./features
) 上調用的;或者以下命令將執行 authenticate_user
功能。features/
目錄子目錄中的任何功能都必須 require
功能。
cucumber --require features features/authentication/authenticate_user.feature
請注意,如果傳遞了 --require
選項,則僅會在該目錄樹中搜尋步驟定義比對。如果您需要從不共用方便根目錄的目錄中包含步驟定義,您可以多次指定 --require
選項。
否則,若要執行所有功能
cucumber
命令列介面執行器 (CLI Runner) 是一個可從命令列執行的 Java 可執行類別。
java io.cucumber.core.cli.Main
請注意,除了已編譯的 .class 檔案的位置外,您還需要將 cucumber-core
jar 及其所有可轉移的相依性新增至您的類別路徑。您可以在 Maven Central 中找到這些 jar。
您還需要透過 --glue
選項 (後面接著其套件名稱) 和功能檔案的路徑,向 CLI 提供您的步驟定義。
例如
java -cp "path/to/each/jar:path/to/compiled/.class/files" io.cucumber.core.cli.Main /path/to/your/feature/files --glue hellocucumber --glue anotherpackage
或者,如果您使用的是 Maven 專案,您可以使用 Exec Maven 外掛程式執行 CLI
mvn exec:java \
-Dexec.classpathScope=test \
-Dexec.mainClass=io.cucumber.core.cli.Main \
-Dexec.args="/path/to/your/feature/files --glue hellocucumber --glue anotherpackage"
命令列介面執行器 (CLI Runner) 是一個可從命令列執行的 Java 可執行類別。
java io.cucumber.core.cli.Main
請注意,除了已編譯的 .class 檔案的位置外,您還需要將 cucumber-core
jar 及其所有可轉移的相依性新增至您的類別路徑。您可以在 Maven Central 中找到這些 jar。
您還需要透過 --glue
選項 (後面接著其套件名稱) 和功能檔案的路徑,向 CLI 提供您的步驟定義。
例如
java -cp "path/to/each/jar:path/to/compiled/.class/files" io.cucumber.core.cli.Main /path/to/your/feature/files --glue hellocucumber --glue anotherpackage
或者,如果您使用的是 Maven 專案,您可以使用 Exec Maven 外掛程式執行 CLI
mvn exec:java \
-Dexec.classpathScope=test \
-Dexec.mainClass=io.cucumber.core.cli.Main \
-Dexec.args="/path/to/your/feature/files --glue hellocucumber --glue anotherpackage"
命令列介面執行器 (CLI Runner) 是一個可從命令列執行的 Java 可執行類別。
java io.cucumber.core.cli.Main
請注意,除了已編譯的 .class 檔案的位置外,您還需要將 cucumber-core
jar 及其所有可轉移的相依性新增至您的類別路徑。您可以在 Maven Central 中找到這些 jar。
您還需要透過 --glue
選項 (後面接著其套件名稱) 和功能檔案的路徑,向 CLI 提供您的步驟定義。
例如
java -cp "path/to/each/jar:path/to/compiled/.class/files" io.cucumber.core.cli.Main /path/to/your/feature/files --glue hellocucumber --glue anotherpackage
或者,如果您使用的是 Maven 專案,您可以使用 Exec Maven 外掛程式執行 CLI
mvn exec:java \
-Dexec.classpathScope=test \
-Dexec.mainClass=io.cucumber.core.cli.Main \
-Dexec.args="/path/to/your/feature/files --glue hellocucumber --glue anotherpackage"
Cucumber.js 包含一個可執行檔案來執行功能。在專案中安裝 Cucumber 後,您可以使用以下方式執行它
$ ./node_modules/.bin/cucumber.js
可執行檔案也別名為 cucumber-js
和 cucumberjs
。
如需更多資訊,請參閱 cucumber-junit-platform-engine 文件。
如需更多資訊,請參閱 cucumber-junit-platform-engine 文件。
如需更多資訊,請參閱 cucumber-junit-platform-engine 文件。
JUnit 5 無法執行 Ruby。
JUnit 5 無法執行 Javascript。
若要使用 JUnit 執行 cucumber 情境,請將 cucumber-junit
相依性新增至您的 pom。
<dependencies>
[...]
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
[...]
</dependencies>
請注意,cucumber-junit
是以 JUnit 4 為基礎。如果您使用的是 JUnit 5,請使用 cucumber-junit-platform-engine 或也包含 junit-vintage-engine
相依性。如需更多資訊,請參閱 JUnit 5 文件
建立使用 Cucumber JUnit 執行器的空類別。
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions()
public class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(Cucumber::class)
@CucumberOptions()
class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(classOf[Cucumber])
@CucumberOptions()
class RunCucumberTest {
}
這將預設執行與執行器位於相同套件中的所有情境,glue 程式碼也假設位於相同的套件中。
@CucumberOptions
可用於為執行器提供額外的設定。
使用外掛程式
例如,如果您想要告訴 Cucumber 使用兩個格式器外掛程式 pretty
和 html
,您可以這樣指定
若要使用 JUnit 執行 cucumber 情境,請將 cucumber-junit
相依性新增至您的 pom。
<dependencies>
[...]
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
[...]
</dependencies>
請注意,cucumber-junit
是以 JUnit 4 為基礎。如果您使用的是 JUnit 5,請使用 cucumber-junit-platform-engine 或也包含 junit-vintage-engine
相依性。如需更多資訊,請參閱 JUnit 5 文件
建立使用 Cucumber JUnit 執行器的空類別。
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions()
public class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(Cucumber::class)
@CucumberOptions()
class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(classOf[Cucumber])
@CucumberOptions()
class RunCucumberTest {
}
這將預設執行與執行器位於相同套件中的所有情境,glue 程式碼也假設位於相同的套件中。
@CucumberOptions
可用於為執行器提供額外的設定。
使用外掛程式
例如,如果您想要告訴 Cucumber 使用兩個格式器外掛程式 pretty
和 html
,您可以這樣指定
若要使用 JUnit 執行 cucumber 情境,請將 cucumber-junit
相依性新增至您的 pom。
<dependencies>
[...]
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
[...]
</dependencies>
請注意,cucumber-junit
是以 JUnit 4 為基礎。如果您使用的是 JUnit 5,請使用 cucumber-junit-platform-engine 或也包含 junit-vintage-engine
相依性。如需更多資訊,請參閱 JUnit 5 文件
建立使用 Cucumber JUnit 執行器的空類別。
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions()
public class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(Cucumber::class)
@CucumberOptions()
class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(classOf[Cucumber])
@CucumberOptions()
class RunCucumberTest {
}
這將預設執行與執行器位於相同套件中的所有情境,glue 程式碼也假設位於相同的套件中。
@CucumberOptions
可用於為執行器提供額外的設定。
使用外掛程式
例如,如果您想要告訴 Cucumber 使用兩個格式器外掛程式 pretty
和 html
,您可以這樣指定
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "html:target/cucumber.html"})
public class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "html:target/cucumber.html"})
class RunCucumberTest
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(classOf[Cucumber])
@CucumberOptions(plugin = Seq("pretty", "html:target/cucumber.html"))
class RunCucumberTest {
}
例如,如果您想要告訴 Cucumber 列印遺失步驟定義的程式碼片段,請使用 summary
外掛程式,您可以這樣指定
例如,如果您想要告訴 Cucumber 列印遺失步驟定義的程式碼片段,請使用 summary
外掛程式,您可以這樣指定
例如,如果您想要告訴 Cucumber 列印遺失步驟定義的程式碼片段,請使用 summary
外掛程式,您可以這樣指定
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "summary"}, snippets = SnippetType.CAMELCASE)
public class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "summary"}, snippets = CAMELCASE)
class RunCucumberTest
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(classOf[Cucumber])
@CucumberOptions(plugin = Seq("pretty", "summary"), snippets = CAMELCASE)
class RunCucumberTest {
}
snippets
的預設選項是 UNDERSCORE
。此設定可用於指定 Cucumber 將如何建立程式碼片段。
執行空跑
例如,如果您想要檢查所有功能檔案步驟是否有對應的步驟定義,您可以這樣指定
snippets
的預設選項是 UNDERSCORE
。此設定可用於指定 Cucumber 將如何建立程式碼片段。
執行空跑
例如,如果您想要檢查所有功能檔案步驟是否有對應的步驟定義,您可以這樣指定
snippets
的預設選項是 UNDERSCORE
。此設定可用於指定 Cucumber 將如何建立程式碼片段。
執行空跑
例如,如果您想要檢查所有功能檔案步驟是否有對應的步驟定義,您可以這樣指定
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(dryRun=true)
public class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(Cucumber.class)
@CucumberOptions(dryRun=true)
class RunCucumberTest
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(classOf[Cucumber])
@CucumberOptions(dryRun=true)
class RunCucumberTest {
}
dryRun
的預設選項是 false
。
設定主控台輸出格式
例如,如果您想要讓 Cucumber 的主控台輸出採用可讀格式,您可以這樣指定
dryRun
的預設選項是 false
。
設定主控台輸出格式
例如,如果您想要讓 Cucumber 的主控台輸出採用可讀格式,您可以這樣指定
dryRun
的預設選項是 false
。
設定主控台輸出格式
例如,如果您想要讓 Cucumber 的主控台輸出採用可讀格式,您可以這樣指定
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(monochrome=true)
public class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(Cucumber.class)
@CucumberOptions(monochrome=true)
class RunCucumberTest
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(classOf[Cucumber])
@CucumberOptions(monochrome=true)
class RunCucumberTest {
}
monochrome
的預設選項是 false
。
使用標籤選取情境
例如,如果您想要告訴 Cucumber 只執行以特定標籤指定的情境,您可以這樣指定
monochrome
的預設選項是 false
。
使用標籤選取情境
例如,如果您想要告訴 Cucumber 只執行以特定標籤指定的情境,您可以這樣指定
monochrome
的預設選項是 false
。
使用標籤選取情境
例如,如果您想要告訴 Cucumber 只執行以特定標籤指定的情境,您可以這樣指定
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(tags = "@foo and not @bar")
public class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(Cucumber.class)
@CucumberOptions(tags = "@foo and not @bar")
class RunCucumberTest
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(classOf[Cucumber])
@CucumberOptions(tags = Seq("@foo and not @bar"))
public class RunCucumberTest {
}
指定物件工廠
例如,如果您將 Cucumber 與 DI 架構搭配使用,並想要使用自訂的物件工廠,您可以這樣指定
指定物件工廠
例如,如果您將 Cucumber 與 DI 架構搭配使用,並想要使用自訂的物件工廠,您可以這樣指定
指定物件工廠
例如,如果您將 Cucumber 與 DI 架構搭配使用,並想要使用自訂的物件工廠,您可以這樣指定
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(objectFactory = FooFactory.class)
public class RunCucumberTest {
}
package com.example
import io.cucumber.junit.Cucumber
import io.cucumber.junit.CucumberOptions
import org.junit.runner.RunWith
@RunWith(Cucumber.class)
@CucumberOptions(objectFactory = FooFactory.class)
class RunCucumberTest
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(classOf[Cucumber])
@CucumberOptions(objectFactory = FooFactory.class)
public class RunCucumberTest {
}
objectFactory
的預設選項是使用預設的物件工廠。有關使用自訂物件工廠的更多資訊,請參閱此處
@CucumberOptions
註釋中還有其他選項可用。
通常,測試類別會是空的。但是,您可以指定數個 JUnit 規則。
Cucumber 執行器就像 JUnit 測試套件。因此,可以預期其他 JUnit 功能 (例如類別、自訂 JUnit 監聽器和報告器) 都能正常運作。
如需有關 JUnit 的更多資訊,請參閱 JUnit 網站
objectFactory
的預設選項是使用預設的物件工廠。有關使用自訂物件工廠的更多資訊,請參閱此處
@CucumberOptions
註釋中還有其他選項可用。
通常,測試類別會是空的。但是,您可以指定數個 JUnit 規則。
Cucumber 執行器就像 JUnit 測試套件。因此,可以預期其他 JUnit 功能 (例如類別、自訂 JUnit 監聽器和報告器) 都能正常運作。
如需有關 JUnit 的更多資訊,請參閱 JUnit 網站
objectFactory
的預設選項是使用預設的物件工廠。有關使用自訂物件工廠的更多資訊,請參閱此處
@CucumberOptions
註釋中還有其他選項可用。
通常,測試類別會是空的。但是,您可以指定數個 JUnit 規則。
Cucumber 執行器就像 JUnit 測試套件。因此,可以預期其他 JUnit 功能 (例如類別、自訂 JUnit 監聽器和報告器) 都能正常運作。
如需有關 JUnit 的更多資訊,請參閱 JUnit 網站
JUnit 無法執行 Ruby。
JUnit 無法執行 Javascript。
Cucumber 提供了數個可以在命令列上傳遞的選項。
例如
若要執行 authenticate_user
功能中第 44 行定義的情境,請將其格式化為 HTML,並將其傳輸到 features.html
檔案,以在瀏覽器中檢視
cucumber features/authenticate_user.feature:44 --format html > features.html
若要執行名為 "Failed login"
的情境
cucumber features --name "Failed login"
您可以列出您正在使用的 Cucumber 版本可用的選項。
假設您已將 Cucumber 安裝為 gem,請在命令提示字元中執行此命令以查看執行功能的選項
cucumber --help
傳遞 --help
選項以印出所有可用的配置選項
java io.cucumber.core.cli.Main --help
傳遞 --help
選項以印出所有可用的配置選項
java io.cucumber.core.cli.Main --help
傳遞 --help
選項以印出所有可用的配置選項
java io.cucumber.core.cli.Main --help
使用 cucumber-js --help
命令來查看哪些參數可以傳遞到可執行檔。
您也可以使用標籤來指定要執行的內容。
Cucumber 會依照優先順序從系統屬性、環境變數和 cucumber.properties
檔案解析屬性。
請注意,@CucumberOptions
提供的選項優先於屬性檔案,而 CLI 參數優先於所有選項。
請注意,cucumber-junit-platform-engine
的屬性是由 Junit Platform 而非 Cucumber 提供。請參閱 junit-platform-engine 配置選項 以取得更多資訊。
例如,如果您使用 Maven 並且想要執行標記為 @smoke
的場景子集
mvn test -Dcucumber.filter.tags="@smoke"
支援的屬性有
cucumber.ansi-colors.disabled= # true or false. default: false
cucumber.execution.dry-run= # true or false. default: false
cucumber.execution.limit= # number of scenarios to execute (CLI only).
cucumber.execution.order= # lexical, reverse, random or random:[seed] (CLI only). default: lexical
cucumber.execution.wip= # true or false. default: false.
cucumber.features= # comma separated paths to feature files. example: path/to/example.feature, path/to/other.feature
cucumber.filter.name= # regex. example: .*Hello.*
cucumber.filter.tags= # tag expression. example: @smoke and not @slow
cucumber.glue= # comma separated package names. example: com.example.glue
cucumber.plugin= # comma separated plugin strings. example: pretty, json:path/to/report.json
cucumber.object-factory= # object factory class name. example: com.example.MyObjectFactory
cucumber.snippet-type= # underscore or camelcase. default: underscore
請注意,篩選選項 cucumber.filter.name
和 cucumber.filter.tags
是使用 and
運算符組合的。換句話說,兩個表達式都需要匹配。
Cucumber 會依照優先順序從系統屬性、環境變數和 cucumber.properties
檔案解析屬性。
請注意,@CucumberOptions
提供的選項優先於屬性檔案,而 CLI 參數優先於所有選項。
請注意,cucumber-junit-platform-engine
的屬性是由 Junit Platform 而非 Cucumber 提供。請參閱 junit-platform-engine 配置選項 以取得更多資訊。
例如,如果您使用 Maven 並且想要執行標記為 @smoke
的場景子集
mvn test -Dcucumber.filter.tags="@smoke"
支援的屬性有
cucumber.ansi-colors.disabled= # true or false. default: false
cucumber.execution.dry-run= # true or false. default: false
cucumber.execution.limit= # number of scenarios to execute (CLI only).
cucumber.execution.order= # lexical, reverse, random or random:[seed] (CLI only). default: lexical
cucumber.execution.wip= # true or false. default: false.
cucumber.features= # comma separated paths to feature files. example: path/to/example.feature, path/to/other.feature
cucumber.filter.name= # regex. example: .*Hello.*
cucumber.filter.tags= # tag expression. example: @smoke and not @slow
cucumber.glue= # comma separated package names. example: com.example.glue
cucumber.plugin= # comma separated plugin strings. example: pretty, json:path/to/report.json
cucumber.object-factory= # object factory class name. example: com.example.MyObjectFactory
cucumber.snippet-type= # underscore or camelcase. default: underscore
請注意,篩選選項 cucumber.filter.name
和 cucumber.filter.tags
是使用 and
運算符組合的。換句話說,兩個表達式都需要匹配。
Cucumber 會依照優先順序從系統屬性、環境變數和 cucumber.properties
檔案解析屬性。
請注意,@CucumberOptions
提供的選項優先於屬性檔案,而 CLI 參數優先於所有選項。
請注意,cucumber-junit-platform-engine
的屬性是由 Junit Platform 而非 Cucumber 提供。請參閱 junit-platform-engine 配置選項 以取得更多資訊。
例如,如果您使用 Maven 並且想要執行標記為 @smoke
的場景子集
mvn test -Dcucumber.filter.tags="@smoke"
支援的屬性有
cucumber.ansi-colors.disabled= # true or false. default: false
cucumber.execution.dry-run= # true or false. default: false
cucumber.execution.limit= # number of scenarios to execute (CLI only).
cucumber.execution.order= # lexical, reverse, random or random:[seed] (CLI only). default: lexical
cucumber.execution.wip= # true or false. default: false.
cucumber.features= # comma separated paths to feature files. example: path/to/example.feature, path/to/other.feature
cucumber.filter.name= # regex. example: .*Hello.*
cucumber.filter.tags= # tag expression. example: @smoke and not @slow
cucumber.glue= # comma separated package names. example: com.example.glue
cucumber.plugin= # comma separated plugin strings. example: pretty, json:path/to/report.json
cucumber.object-factory= # object factory class name. example: com.example.MyObjectFactory
cucumber.snippet-type= # underscore or camelcase. default: underscore
請注意,篩選選項 cucumber.filter.name
和 cucumber.filter.tags
是使用 and
運算符組合的。換句話說,兩個表達式都需要匹配。
您也可以在 cucumber.yml
檔案中定義常用的命令列選項。
有關如何配置選項的更多資訊,請查看 GitHub 上的 cucumber-js 文件
您可以幫助我們改進此文件。編輯此頁面。