型レジストリ
パラメータ型を使用すると、Cucumber 式のパラメータをオブジェクトに変換できます。データテーブルとドックストリング型を使用すると、データテーブルとドックストリングをオブジェクトに変換できます。ステップ定義と同様に、型定義はグルーの一部です。グルーパスに配置すると、Cucumber はそれらを自動的に検出します。
パラメータ型を使用すると、Cucumber 式のパラメータをオブジェクトに変換できます。データテーブルとドックストリング型を使用すると、データテーブルとドックストリングをオブジェクトに変換できます。ステップ定義と同様に、型定義はグルーの一部です。グルーパスに配置すると、Cucumber はそれらを自動的に検出します。
パラメータ型を使用すると、Cucumber 式のパラメータをオブジェクトに変換できます。データテーブルとドックストリング型を使用すると、データテーブルとドックストリングをオブジェクトに変換できます。ステップ定義と同様に、型定義はグルーの一部です。グルーパスに配置すると、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
}
}
ラムダ定義のステップ定義の場合、DataTableType
、ParameterType
、および DocStringType
関数があります
ラムダ定義のステップ定義の場合、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))
}
}
ラムダ定義のステップ定義の場合、DefaultParameterTransformer
、DefaultDataTableCellTransformer
、および DefaultDataTableEntryTransformer
メソッド関数ブロック関数関数 があります
ラムダ定義のステップ定義の場合、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 ドキュメント を参照してください。
必要に応じて、Jackson Scala モジュールを使用してデフォルトのトランスフォーマーを定義する、定義済みの JacksonDefaultDataTableEntryTransformer
トレイトを使用できます。
デフォルトの Jackson データテーブルトランスフォーマーを参照してください。
推奨される場所
カスタムパラメータ型を定義する推奨場所は、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 を使用して同様の動作を模倣するには、Maven プロファイルとプロパティの移行に関する Gradle ドキュメントを参照してください。
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 を使用して同様の動作を模倣するには、Maven プロファイルとプロパティの移行に関する Gradle ドキュメントを参照してください。
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 を使用して同様の動作を模倣するには、Maven プロファイルとプロパティの移行に関する Gradle ドキュメントを参照してください。
Cucumber-js でプロファイルを使用する方法の詳細については、profiles.feature を参照してください。
Cucumber の構成オプションは、cucumber.yml
または cucumber.yaml
ファイルで指定できます。このファイルは、現在の作業ディレクトリの .config
ディレクトリまたは config
サブディレクトリに存在する必要があります。
config/cucumber.yml
## ##YAML Template
html_report: --format progress --format html --out=features_report.html
bvt: --tags @bvt
テンプレートを定義するには、名前と、このプロファイルで実行するコマンドラインオプションが必要です。
上記の例では、2 つのプロファイルが生成されます
- 新しい出力形式を指定するコマンドラインオプションのリストを持つ
html_report
と @bvt
のタグが付いたすべてのフィーチャーとシナリオを実行するbvt
。
Cucumber-Rails は、プロジェクトの config ディレクトリに、定義済みのプロファイルをいくつか含む cucumber.yml
ファイルを作成します。そのうちの 1 つがデフォルトプロファイルです。通常、コマンドラインから 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
複数のプロファイルを一緒に指定することもできます。次は、指定された進行状況と HTML 出力で、タグ @bvt
が付いたすべてのフィーチャーとシナリオを実行します。
\[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
プロファイルを指定すると、異なるプロファイルを明示的に指定しない場合は常に、default
コマンドラインオプションを使用するように Cucumber に指示することになります。
同じ例を使用して、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
プロファイルの両方を使用し、進行状況出力と HTML 出力とともに、@bvt
のタグが付いたすべてのフィーチャーとシナリオをテストします。
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 file
の先頭に配置する必要があります。
features/support で見つかった .rb
で終わるすべてのファイルは、Cucumber によってロードされます。したがって、ローカルのカスタマイズをそのディレクトリの任意の .rb
ファイルに配置すると、それらがロードされます。ただし、Cucumber < 4.x では、--dry-run
オプションは、/env\\..\*/
の正規表現に一致する features/support
内のファイルのみを除外することに注意してください(末尾のドットは重要です)。したがって、my_locals.rb
という名前のローカルカスタマイズを含むファイルは、ロードされます。Cucumber 4.x では、env.rb
ファイルを含むすべてのサポートファイルがロードされます。
Cucumberでドライランを実行する際に読み込みたくないカスタムファイルを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
への変更を差分比較し、マージする準備をしておく必要があります。