類型註冊表
參數類型可讓您將參數從 cucumber-expressions 轉換為物件。資料表和文件字串類型可讓您將資料表和文件字串轉換為物件。與步驟定義一樣,類型定義是膠合的一部分。放置在膠合路徑上時,Cucumber 會自動偵測到它們。
參數類型可讓您將參數從 cucumber-expressions 轉換為物件。資料表和文件字串類型可讓您將資料表和文件字串轉換為物件。與步驟定義一樣,類型定義是膠合的一部分。放置在膠合路徑上時,Cucumber 會自動偵測到它們。
參數類型可讓您將參數從 cucumber-expressions 轉換為物件。資料表和文件字串類型可讓您將資料表和文件字串轉換為物件。與步驟定義一樣,類型定義是膠合的一部分。放置在膠合路徑上時,Cucumber 會自動偵測到它們。
例如,以下類別會註冊自訂的「Author」資料表類型
例如,以下類別會註冊自訂的「Author」資料表類型
例如,以下類別會註冊自訂的「Author」資料表類型
package com.example;
import io.cucumber.java.DataTableType;
import io.cucumber.java.en.Given;
import java.util.List;
import java.util.Map;
public class StepDefinitions {
@DataTableType
public Author authorEntry(Map<String, String> entry) {
return new Author(
entry.get("firstName"),
entry.get("lastName"),
entry.get("famousBook"));
}
@Given("There are my favorite authors")
public void these_are_my_favourite_authors(List<Author> authors) {
// step implementation
}
}
package com.example
import io.cucumber.java.DataTableType
import io.cucumber.java.en.Given
import kotlin.streams.toList
class StepDefinitions {
@DataTableType
fun authorEntry(entry: Map<String, String>): Author {
return Author(
entry["firstName"],
entry["lastName"],
entry["famousBook"])
}
@Given("There are my favorite authors")
fun these_are_my_favourite_authors(authors: List<Author>) {
// step implementation
}
}
package com.example
import io.cucumber.scala.{ScalaDsl, EN}
class StepDefinitions extends ScalaDsl with EN {
DataTableType { entry: Map[String, String] =>
Author(
entry("firstName"),
entry("lastName"),
entry("famousBook"))
}
Given("There are my favorite authors") { authors: List[Author] =>
// step implementation
}
}
參數類型範例
參數類型範例
參數類型範例
package com.example;
import io.cucumber.java.ParameterType;
import io.cucumber.java.en.Given;
public class StepDefinitions {
@ParameterType(".*")
public Book book(String bookName) {
return new Book(bookName);
}
@Given("{book} is my favorite book")
public void this_is_my_favorite_book(Book book) {
// step implementation
}
}
package com.example
import io.cucumber.java.ParameterType
import io.cucumber.java.en.Given
class StepDefinitions {
@ParameterType(".*")
fun book(bookName: String): Book {
return Book(bookName)
}
@Given("{book} is my favorite book")
fun this_is_my_favorite_book(book: Book) {
// step implementation
}
}
package com.example
import io.cucumber.scala.{ScalaDsl, EN}
class StepDefinitions extends ScalaDsl with EN {
ParameterType("book", ".*") { bookName: String =>
Book(bookName)
}
Given("{book} is my favorite book") { book: Book =>
// step implementation
}
}
文件字串類型範例
文件字串類型範例
文件字串類型範例
package com.example;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cucumber.java.DocStringType;
import io.cucumber.java.en.Given;
public class StepsDefinitions {
private static ObjectMapper objectMapper = new ObjectMapper();
@DocStringType
public JsonNode json(String docString) throws JsonProcessingException {
return objectMapper.readValue(docString, JsonNode.class);
}
@Given("Books are defined by json")
public void books_are_defined_by_json(JsonNode books) {
// step implementation
}
}
package com.example
import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import io.cucumber.java.DocStringType
import io.cucumber.java.en.Given
class StepsDefinitions {
companion object {
private val objectMapper = ObjectMapper()
}
@DocStringType
@Throws(JsonProcessingException::class)
fun json(docString: String): JsonNode {
return objectMapper.readValue(docString, JsonNode::class)
}
@Given("Books are defined by json")
fun books_are_defined_by_json(books: JsonNode) {
// step implementation
}
}
package com.example
import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper
import io.cucumber.scala.{ScalaDsl, EN}
object StepsDefinitions {
private val objectMapper = ObjectMapper()
}
class StepsDefinitions extends ScalaDsl with EN {
DocStringType("json") { docString: String =>
objectMapper.readValue(docString, classOf[JsonNode])
}
Given("Books are defined by json") { books: JsonNode =>
// step implementation
}
}
對於 Lambda 定義的步驟定義,有 DataTableType
、ParameterType
和 DocStringType
函式
對於 Lambda 定義的步驟定義,有 DataTableType
、ParameterType
和 DocStringType
函式
package com.example;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cucumber.java8.En;
import java.util.Map;
public class LambdaStepDefinitions implements En {
private static ObjectMapper objectMapper = new ObjectMapper();
public LambdaStepDefinitions() {
DataTableType((Map<String, String> entry) -> new Author(
entry.get("firstName"),
entry.get("lastName"),
entry.get("famousBook")
));
ParameterType("book", ".*", (String bookName) -> new Book(bookName));
DocStringType("json", (String docString) ->
objectMapper.readValue(docString, JsonNode.class));
}
}
package com.example
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import io.cucumber.java8.En
class LambdaStepDefinitions : En {
init {
val objectMapper = ObjectMapper()
ParameterType("book", ".*") { s : String ->
Book(s)
}
DataTableType { entry: Map<String, String> ->
Author(entry["firstName"], entry["lastName"], entry["famousBook"])
}
DocStringType("json") { docString: String ->
objectMapper.readValue(docString, JsonNode::class)
}
}
}
使用 @DefaultParameterTransformer
、@DefaultDataTableEntryTransformer
和 @DefaultDataTableCellTransformer
註解,也可以插入 ObjectMapper。物件對應器(本範例中為 Jackson)將會處理匿名參數類型和資料表項目的轉換。
使用 @DefaultParameterTransformer
、@DefaultDataTableEntryTransformer
和 @DefaultDataTableCellTransformer
註解,也可以插入 ObjectMapper。物件對應器(本範例中為 Jackson)將會處理匿名參數類型和資料表項目的轉換。
使用 DefaultParameterTransformer
、DefaultDataTableEntryTransformer
和 DefaultDataTableCellTransformer
方法,也可以插入 ObjectMapper。物件對應器(本範例中為 Jackson)將會處理匿名參數類型和資料表項目的轉換。
package com.example;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cucumber.java.DefaultDataTableCellTransformer;
import io.cucumber.java.DefaultDataTableEntryTransformer;
import io.cucumber.java.DefaultParameterTransformer;
import java.lang.reflect.Type;
public class StepDefinitions {
private final ObjectMapper objectMapper = new ObjectMapper();
@DefaultParameterTransformer
@DefaultDataTableEntryTransformer
@DefaultDataTableCellTransformer
public Object transformer(Object fromValue, Type toValueType) {
return objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType));
}
}
package com.example
import com.fasterxml.jackson.databind.ObjectMapper
import io.cucumber.java.DefaultDataTableCellTransformer
import io.cucumber.java.DefaultDataTableEntryTransformer
import io.cucumber.java.DefaultParameterTransformer
import java.lang.reflect.Type
class StepDefinitions {
private val objectMapper = ObjectMapper()
@DefaultParameterTransformer
@DefaultDataTableEntryTransformer
@DefaultDataTableCellTransformer
fun transformer(fromValue: Any, toValueType: Type): Any {
return objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType))
}
}
package com.example
import com.fasterxml.jackson.databind.ObjectMapper
import io.cucumber.scala.ScalaDsl
import java.lang.reflect.Type
class StepDefinitions extends ScalaDsl {
private val objectMapper = ObjectMapper()
DefaultParameterTransformer { (fromValue: String, toValueType: Type) =>
objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType))
}
DefaultDataTableCellTransformer { (fromValue: String, toValueType: Type) =>
objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType))
}
DefaultDataTableEntryTransformer { (fromValue: Map[String, String], toValueType: Type) =>
objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType))
}
}
對於 Lambda 定義的步驟定義,有 DefaultParameterTransformer
、DefaultDataTableCellTransformer
和 DefaultDataTableEntryTransformer
方法函式區塊函式函式
對於 Lambda 定義的步驟定義,有 DefaultParameterTransformer
、DefaultDataTableCellTransformer
和 DefaultDataTableEntryTransformer
方法函式區塊函式函式
package com.example;
import io.cucumber.java8.En;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.lang.reflect.Type;
public class LambdaStepDefinitions implements En {
public LambdaStepDefinitions() {
ObjectMapper objectMapper = new ObjectMapper();
DefaultParameterTransformer((String fromValue, Type toValueType) ->
objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType)));
DefaultDataTableCellTransformer((fromValue, toValueType) ->
objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType)));
DefaultDataTableEntryTransformer((fromValue, toValueType) ->
objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType)));
}
}
import com.fasterxml.jackson.databind.ObjectMapper
import io.cucumber.java8.En
import java.lang.reflect.Type
class LambdaStepDefinitions : En {
init {
val objectMapper = ObjectMapper()
DefaultParameterTransformer { fromValue: String, toValueType: Type ->
objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType))
}
DefaultDataTableCellTransformer { fromValue, toValueType ->
objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType))
}
DefaultDataTableEntryTransformer { fromValue, toValueType ->
objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType))
}
}
}
參數類型
可讓您在將引數傳遞至步驟定義之前,將引數從字串轉換為其他類型。
例如,讓我們定義我們自己的「Person」類型
ParameterType(
name: 'person',
regexp: /[A-Z][a-z]+/,
transformer: -> (name) { Person.new(name) }
)
以下是如何在步驟定義中使用此項的範例
Then /^a user {person} should have {int} followers$/ do |person, count|
assert(person.is_a?(Person))
end
如果您使用尚未定義的類型,您將會收到類似以下的錯誤
The parameter type "person" is not defined.
如果您使用尚未定義的類型,您將會收到類似以下的錯誤
The parameter type "person" is not defined.
如果您使用尚未定義的類型,您將會收到類似以下的錯誤
The parameter type "person" is not defined.
如果您使用尚未定義的類型,您將會收到類似以下的錯誤
The parameter type "person" is not defined.
您可以定義自己的參數類型和資料表類型。
如需瞭解如何在 Cucumber-js 中使用 參數類型
的更多資訊,請參閱parameter_types.feature。
如需瞭解如何在 Cucumber-js 中使用 資料表
的更多資訊,請參閱cucumber-js 文件。
如果您想要,可以使用預先定義的 JacksonDefaultDataTableEntryTransformer
特徵,其定義使用 Jackson Scala 模組的預設轉換器。
建議位置
定義自訂參數類型的建議位置會在features/support/parameter_types.rb
。features/support/parameter_types.js
。src/test/java/com/example/ParameterTypes.java
。src/test/kotlin/com/example/ParameterTypes.kt
。src/test/kotlin/com/example/ParameterTypes.scala
。這只是一個慣例;Cucumber 會從任何features 底下的features 底下的在膠合路徑上的在膠合路徑上的在膠合路徑上的檔案中擷取它們。
設定檔
Cucumber 設定檔在 Cucumber-JVM 上無法使用。但是,可以使用 Maven 設定檔設定組態選項
例如,我們可以針對要在不同環境中執行的情境設定不同的設定檔,如下所示
<profiles>
<profile>
<id>dev</id>
<properties>
<cucumber.filter.tags>@dev and not @ignore</cucumber.filter.tags>
</properties>
</profile>
<profile>
<id>qa</id>
<properties>
<cucumber.filter.tags>@qa</cucumber.filter.tags>
</properties>
</profile>
</profiles>
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<systemPropertyVariables>
<cucumber.filter.tags>${cucumber.filter.tags}</cucumber.filter.tags>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
若要使用 Gradle 模擬類似的行為,請參閱 Gradle 文件中的遷移 Maven 設定檔和屬性。
Cucumber 設定檔在 Cucumber-JVM 上無法使用。但是,可以使用 Maven 設定檔設定組態選項
例如,我們可以針對要在不同環境中執行的情境設定不同的設定檔,如下所示
<profiles>
<profile>
<id>dev</id>
<properties>
<cucumber.filter.tags>@dev and not @ignore</cucumber.filter.tags>
</properties>
</profile>
<profile>
<id>qa</id>
<properties>
<cucumber.filter.tags>@qa</cucumber.filter.tags>
</properties>
</profile>
</profiles>
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<systemPropertyVariables>
<cucumber.filter.tags>${cucumber.filter.tags}</cucumber.filter.tags>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
若要使用 Gradle 模擬類似的行為,請參閱 Gradle 文件中的遷移 Maven 設定檔和屬性。
Cucumber 設定檔在 Cucumber-JVM 上無法使用。但是,可以使用 Maven 設定檔設定組態選項
例如,我們可以針對要在不同環境中執行的情境設定不同的設定檔,如下所示
<profiles>
<profile>
<id>dev</id>
<properties>
<cucumber.filter.tags>@dev and not @ignore</cucumber.filter.tags>
</properties>
</profile>
<profile>
<id>qa</id>
<properties>
<cucumber.filter.tags>@qa</cucumber.filter.tags>
</properties>
</profile>
</profiles>
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<systemPropertyVariables>
<cucumber.filter.tags>${cucumber.filter.tags}</cucumber.filter.tags>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
若要使用 Gradle 模擬類似的行為,請參閱 Gradle 文件中的遷移 Maven 設定檔和屬性。
如需瞭解如何在 Cucumber-js 中使用設定檔的更多資訊,請參閱profiles.feature。
您可以在 cucumber.yml
或 cucumber.yaml
檔案中指定 Cucumber 的組態選項。此檔案必須位於 .config
目錄或目前工作目錄的 config
子目錄中。
config/cucumber.yml
## ##YAML Template
html_report: --format progress --format html --out=features_report.html
bvt: --tags @bvt
定義範本需要名稱,然後是您想要使用此設定檔執行的命令列選項。
以上範例會產生兩個設定檔
html_report
,其中包含指定新輸出格式的命令列選項清單,以及bvt
,其會執行所有標記為@bvt
的功能和情境。
Cucumber-Rails 會在專案組態目錄中建立 cucumber.yml
檔案,其中包含許多預先定義的設定檔,其中一個是預設設定檔。從命令列執行 Cucumber 時,通常必須同時提供包含功能檔案樹狀結構根目錄的目錄名稱,以及包含必要程式庫檔案參考的目錄名稱。在典型的專案中,cucumber --require features features/some/path
即可。重複的用法可以新增至專案 cucumber.yml
檔案中包含的使用者定義設定檔。
若要執行設定檔,請使用
\[user@system project] cucumber --profile html_report
\[user@system project] cucumber -p bvt
使用旗標 --profile
或 -p
以使用設定檔執行 Cucumber。如果需要,您仍然可以搭配 --profile
或 -p
使用其他命令列引數。
\[user@system project] cucumber --profile html_report --tags ~@wip
甚至可以一起指定多個設定檔。以下會執行所有標記為 @bvt
的功能和情境,以及指定的進度和 HTML 輸出。
\[user@system project] cucumber -p html_report -p bvt
預設設定檔
Cucumber 設定檔在 Cucumber-JVM 上無法使用。請參閱上方。
Cucumber 設定檔在 Cucumber-JVM 上無法使用。請參閱上方。
Cucumber 設定檔在 Cucumber-JVM 上無法使用。請參閱上方。
如需瞭解如何在 Cucumber-js 中使用設定檔的更多資訊,請參閱profiles.feature。
您很可能會在大多數情況下使用特定設定檔執行 Cucumber。Cucumber 組態檔使用 default
設定檔來提供此功能。當您指定 default
設定檔時,您是在告訴 Cucumber 在您未明確指定其他設定檔時,使用 default
命令列選項。
使用相同的範例,也許我們希望 html_report
設定檔是我們的預設執行。
1. config/cucumber.yml
## ##YAML Template
default: --profile html_report --profile bvt
html_report: --format progress --format html --out=features_report.html
bvt: --tags @bvt
\[user@system project] cucumber
使用此設定,Cucumber 現在將同時使用 bvt
設定檔和 html_report
設定檔,測試所有標記為 @bvt
的功能和情境,以及進度輸出和 HTML 輸出。
使用 ERB 進行預處理
ERB (Embedded RuBy) 是一種 Ruby 特定工具。
ERB (Embedded RuBy) 是一種 Ruby 特定工具。
ERB (Embedded RuBy) 是一種 Ruby 特定工具。
ERB (Embedded RuBy) 是一種 Ruby 特定工具。
cucumber.yml
檔案由 ERB (Embedded RuBy) 進行預處理。這可讓您使用 Ruby 程式碼在 cucumber.yml
檔案中產生值。
因此,如果您有數個具有相似值的設定檔,您可以執行此動作
1. config/cucumber.yml
## ##YAML Template
<% common = "--tags ~@wip --strict" %>
default: <%= common %> features
html_report: <%= common %> --format html --out=features_report.html features
環境變數
Cucumber-JVM 不支援使用 env
檔案設定 Cucumber。
Cucumber-JVM 不支援使用 env
檔案設定 Cucumber。
Cucumber-JVM 不支援使用 env
檔案設定 Cucumber。
Cucumber-js 不支援使用 env
檔案設定 Cucumber。
您可以在設定檔引數清單中使用環境變數,就像您通常在命令列上指定它們一樣。
1. config/cucumber.yml
\##YAML Template
2. ## ie profile executes the browser features with Internet Explorer
default: --profile html_report --profile bvt
html_report: --format progress --format html --out=features_report.html
bvt: --tags @bvt
ie: BROWSER=IE
當執行 Cucumber時,有時將特殊值傳遞至 Cucumber 供您的步驟定義使用會很方便。
您可以在命令列中執行此動作
cucumber FOO=BAR --format progress features
您現在可以在 Ruby 中擷取 ENV\['FOO']
(例如,在 env.rb
或步驟定義中),並根據值執行動作。
您也可以在 cucumber.yml
中執行此動作。
例如,以下設定一個設定檔,該設定檔會執行指定的標籤並設定環境變數
baz: --tags @mytag FOO=BAR
在 support/env.rb
中的本機 Cucumber 自訂程式碼,因為該檔案通常會被 script/generate cucumber:install | rails g cucumber
覆寫。必須在 Cucumber 初始化其他部分之前載入的自訂設定,必須放在 env.rb
檔案的開頭。
Cucumber 會載入在 features/support 中找到的每個以 .rb
結尾的檔案。因此,如果您將本機自訂設定放在該目錄中的任何 .rb
檔案中,它們將會被載入。但是,請注意,在 Cucumber 4.x 之前的版本中,--dry-run
選項只會排除 features/support
中符合正規表示式 /env\\..\*/
的檔案(請注意,尾隨的點號很重要)。因此,一個名為 my_locals.rb
的本機自訂設定檔案將會被載入,無論如何。在 Cucumber 4.x 中,所有支援檔案,包括 env.rb
檔案,都會被載入。
如果您將不想在用 Cucumber 進行 dry-run 時載入的自訂檔案放在 features/support
內,您可以使用 --exclude
旗標來確保它們不會被載入。在 Cucumber 4.x 之前的版本中,您也可以將檔名前加上 env
,例如 env.local.rb
。請注意,例如,名為 local_env.rb
的檔案不符合上述的正規表示式,因此除非明確排除,否則會在所有版本的 Cucumber 中載入。
作為良好的實務,您應該在每次安裝 Cucumber 或 cucumber-rails 的更新版本時,執行 script/generate cucumber | rails g cucumber:install
。然而,這會覆寫 features/support/env.rb
。為了保留您 env.rb
檔案中的任何自訂設定,請將您的 env.rb
與其他版本控制的檔案一起簽入,並準備好在 Cucumber-Rails 版本之間差異化和合併 env.rb
的變更。