例のマッピング
例のマッピングワークショップをチーム内で開催し、例を一緒に設計してみましょう。
このクイックチュートリアルでは、以下を学習します
Cucumberを使用して、金曜日かどうかを判断できる小さなライブラリを開発します。
このチュートリアルでは、以下を前提としていますのでご注意ください
Gemfile
の基本的な理解始める前に、以下が必要です
Node.jsが正しくインストールされていることを確認するために、ターミナルを開きます
node -v
npm -v
これらの両方のコマンドはバージョン番号を出力するはずです。
Rubyが正しくインストールされていることを確認するために、ターミナルを開きます
ruby -v
bundle -v
これらの両方のコマンドはバージョン番号を出力するはずです。
最初に、cucumber-archetype
Mavenプラグインを使用して新しいプロジェクトディレクトリを作成します。ターミナルを開き、プロジェクトを作成するディレクトリに移動して、次のコマンドを実行します
mvn archetype:generate \
"-DarchetypeGroupId=io.cucumber" \
"-DarchetypeArtifactId=cucumber-archetype" \
"-DarchetypeVersion=7.20.1" \
"-DgroupId=hellocucumber" \
"-DartifactId=hellocucumber" \
"-Dpackage=hellocucumber" \
"-Dversion=1.0.0-SNAPSHOT" \
"-DinteractiveMode=false"
次のような結果が得られます
[INFO] Project created from Archetype in dir: <directory where you created the project>/cucumber
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
次のコマンドを実行して、新しく作成されたディレクトリに変更します
cd hellocucumber
プロジェクトをIntelliJ IDEAで開きます
最初に、cucumber-archetype
Mavenプラグインを使用して新しいプロジェクトディレクトリを作成します。ターミナルを開き、プロジェクトを作成するディレクトリに移動して、次のコマンドを実行します
mvn archetype:generate \
"-DarchetypeGroupId=io.cucumber" \
"-DarchetypeArtifactId=cucumber-archetype" \
"-DarchetypeVersion=7.20.1" \
"-DgroupId=hellocucumber" \
"-DartifactId=hellocucumber" \
"-Dpackage=hellocucumber" \
"-Dversion=1.0.0-SNAPSHOT" \
"-DinteractiveMode=false"
次のような結果が得られます
[INFO] Project created from Archetype in dir: <directory where you created the project>/cucumber
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
次のコマンドを実行して、新しく作成されたディレクトリに変更します
cd hellocucumber
プロジェクトをIntelliJ IDEAで開きます
Kotlinを使用するために、プロジェクトに追加する必要があります
src/test
ディレクトリにkotlin
という名前のディレクトリを追加し、Test Sources Root
としてマークします。IntelliJ IDEAでは、kotlin
ディレクトリを右クリックして、「ディレクトリをマークとして」>「Test Sources Root」を選択することでできます。kotlin
ディレクトリ内にhellocucumber
パッケージを作成します。hellocucumber
パッケージ内にRunCucumberTest
という名前のKotlinクラスを作成します。IntelliJ IDEAはKotlinが設定されていないことを伝える場合があります。「構成」をクリックします。pom.xml
は次のようになります<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hellocucumber</groupId>
<artifactId>hellocucumber</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
<source>src/test/kotlin</source>
</sourceDirs>
</configuration>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-Werror</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.2.71</kotlin.version>
</properties>
</project>
RunCucumberTest.java
クラスからRunCucumberTest.kt
クラスにコピーします。IntelliJ IDEAを使用している場合、JavaコードをKotlinコードに変換することを提案します。それ以外の場合は自分で書く必要があります。RunCucumberTest.kt
クラスは次のようになります
package hellocucumber
import io.cucumber.junit.CucumberOptions
import io.cucumber.junit.Cucumber
import org.junit.runner.RunWith
@RunWith(Cucumber::class)
@CucumberOptions(plugin = ["pretty"])
class RunCucumberTest
RunCucumberTest.java
クラスを削除できます。hellocucumber
パッケージ内にStepDefs
という名前のKotlinクラスを作成します。StepDefinitions.java
からStepDefs.kt
にコピーします。後で必要になります。StepDefinitions.java
クラス(またはjava
ディレクトリさえ)を削除します。このプロジェクトでKotlinを使用するには、いくつかの追加手順を実行する必要があります
src/test
ディレクトリにkotlin
という名前のディレクトリを追加し、Test Sources Root
としてマークします。IntelliJ IDEAでは、kotlin
ディレクトリを右クリックして、「ディレクトリをマークとして」>「Test Sources Root」を選択することでできます。kotlin
ディレクトリ内にhellocucumber
パッケージを作成します。hellocucumber
パッケージ内にRunCucumberTest
という名前のKotlinクラスを作成し、アノテーションをRunCucumberTest.java
クラスからRunCucumberTest.kt
クラスにコピーします。IntelliJ IDEAを使用している場合、JavaコードをKotlinコードに変換することを提案します。それ以外の場合は自分で書く必要があります。RunCucumberTest.kt
クラスは次のようになります
package hellocucumber
import io.cucumber.junit.CucumberOptions
import io.cucumber.junit.Cucumber
import org.junit.runner.RunWith
@RunWith(Cucumber::class)
@CucumberOptions(plugin = ["pretty"])
class RunCucumberTest
新しいディレクトリと空のNode.jsプロジェクトを作成します。
mkdir hellocucumber
cd hellocucumber
npm init --yes
開発依存関係としてCucumberを追加します
npm install --save-dev @cucumber/cucumber
テキストエディタでpackage.json
を開き、test
セクションを次のように変更します
{
"name": "hellocucumber",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "cucumber-js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"cucumber": "^10.9.0"
}
}
ファイル構造を準備します
mkdir features
mkdir features/step_definitions
プロジェクトのルートにcucumber.js
という名前のファイルを作成し、次のコンテンツを追加します
module.exports = {
default: `--format-options '{"snippetInterface": "synchronous"}'`
}
さらに、次のコンテンツを使用してfeatures/step_definitions/stepdefs.js
という名前のファイルを作成します
const assert = require('assert');
const { Given, When, Then } = require('@cucumber/cucumber');
新しいディレクトリと空のRubyプロジェクトを作成します。
mkdir hellocucumber
cd hellocucumber
次のコンテンツを使用してGemfile
を作成します
source "https://rubygems.org"
group :test do
gem 'cucumber', '~> 9.2.0'
gem 'rspec', '~> 3.13.0'
end
Cucumberをインストールし、ファイル構造を準備します
bundle install
cucumber --init
これで、Cucumberがインストールされた小さなプロジェクトができました。
すべてが正しく連携するかを確認するために、Cucumberを実行しましょう。
mvn test
mvn test
# Run via NPM
npm test
# Run standalone
npx cucumber-js
cucumber
次のように表示されます
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
0 Scenarios
0 steps
0m00.000s
0 scenarios
0 steps
0m0.000s
Cucumberの出力は、実行するものが何も見つからないことを示しています。
Cucumber で行動駆動開発を行うとき、ソフトウェアで行ってほしい具体的な例を使用して何を指定します。シナリオは商品コードより前に記載されます。実行可能な仕様として書かれ始めます。商品コードが明らかになると、シナリオは生きたドキュメント、自動テストとして役割を果たします。
Cucumber では、例はシナリオと呼ばれています。.feature
ファイルにシナリオを定義します。これらのファイルは src/test/resources/hellocucumber
features
features
ディレクトリ(またはサブディレクトリ)に格納されています。
具体的な例として、日曜日には金曜日は来ないがあります。
次のコンテンツを含んだ、src/test/resources/hellocucumber/is_it_friday_yet.feature
という空のファイルを作成します
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday
Given today is Sunday
When I ask whether it's Friday yet
Then I should be told "Nope"
このファイルの最初の行はキーワード Feature:
から始まり、その後に名前が続きます。ファイル名とよく似た名前を使用することをお勧めします。
2行目は fiturの短い説明です。ドキュメントであるため、Cucumber はこの行を実行しません。
4行目、Scenario: 日曜日に金曜日は来ない
は、ソフトウェアがどのように動作する必要があるのかを示す具体的な例であるシナリオです。
Given(前提条件)
、When(条件)
、Then(結果)
で始まる最後の 3 行は、シナリオのステップです。Cucumber が実行する内容です。
シナリオが用意できたので、Cucumber に処理を依頼できます。
mvn test
mvn test
npm test
cucumber
Cucumber は、未定義
シナリオが 1 つあり、未定義
ステップが 3 つあることを示します。また、これらのステップを定義するために使用できるコードのスニペットも示します
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday
When I ask whether it's Friday yet
Then I should be told "Nope"
┌───────────────────────────────────────────────────────────────────────────────────┐
│ Share your Cucumber Report with your team at https://reports.cucumber.io │
│ Activate publishing with one of the following: │
│ │
│ src/test/resources/cucumber.properties: cucumber.publish.enabled=true │
│ src/test/resources/junit-platform.properties: cucumber.publish.enabled=true │
│ Environment variable: CUCUMBER_PUBLISH_ENABLED=true │
│ JUnit: @CucumberOptions(publish = true) │
│ │
│ More information at https://cucumber.dokyumento.jp/docs/cucumber/environment-variables/ │
│ │
│ Disable this message with one of the following: │
│ │
│ src/test/resources/cucumber.properties: cucumber.publish.quiet=true │
│ src/test/resources/junit-platform.properties: cucumber.publish.quiet=true │
└───────────────────────────────────────────────────────────────────────────────────┘
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.15 s <<< FAILURE! - in hellocucumber.RunCucumberTest
[ERROR] Is it Friday yet?.Sunday isn't Friday Time elapsed: 0.062 s <<< ERROR!
io.cucumber.junit.platform.engine.UndefinedStepException:
The step 'today is Sunday' and 2 other step(s) are undefined.
You can implement these steps using the snippet(s) below:
@Given("today is Sunday")
public void today_is_sunday() {
// Write code here that turns the phrase above into concrete actions
throw new io.cucumber.java.PendingException();
}
@When("I ask whether it's Friday yet")
public void i_ask_whether_it_s_friday_yet() {
// Write code here that turns the phrase above into concrete actions
throw new io.cucumber.java.PendingException();
}
@Then("I should be told {string}")
public void i_should_be_told(String string) {
// Write code here that turns the phrase above into concrete actions
throw new io.cucumber.java.PendingException();
}
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday
When I ask whether it's Friday yet
Then I should be told "Nope"
┌───────────────────────────────────────────────────────────────────────────────────┐
│ Share your Cucumber Report with your team at https://reports.cucumber.io │
│ Activate publishing with one of the following: │
│ │
│ src/test/resources/cucumber.properties: cucumber.publish.enabled=true │
│ src/test/resources/junit-platform.properties: cucumber.publish.enabled=true │
│ Environment variable: CUCUMBER_PUBLISH_ENABLED=true │
│ JUnit: @CucumberOptions(publish = true) │
│ │
│ More information at https://cucumber.dokyumento.jp/docs/cucumber/environment-variables/ │
│ │
│ Disable this message with one of the following: │
│ │
│ src/test/resources/cucumber.properties: cucumber.publish.quiet=true │
│ src/test/resources/junit-platform.properties: cucumber.publish.quiet=true │
└───────────────────────────────────────────────────────────────────────────────────┘
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.15 s <<< FAILURE! - in hellocucumber.RunCucumberTest
[ERROR] Is it Friday yet?.Sunday isn't Friday Time elapsed: 0.062 s <<< ERROR!
io.cucumber.junit.platform.engine.UndefinedStepException:
The step 'today is Sunday' and 2 other step(s) are undefined.
You can implement these steps using the snippet(s) below:
@Given("today is Sunday")
public void today_is_sunday() {
// Write code here that turns the phrase above into concrete actions
throw new io.cucumber.java.PendingException();
}
@When("I ask whether it's Friday yet")
public void i_ask_whether_it_s_friday_yet() {
// Write code here that turns the phrase above into concrete actions
throw new io.cucumber.java.PendingException();
}
@Then("I should be told {string}")
public void i_should_be_told(String string) {
// Write code here that turns the phrase above into concrete actions
throw new io.cucumber.java.PendingException();
}
UUU
Warnings:
1) Scenario: Sunday is not Friday # features/is_it_friday_yet.feature:4
? Given today is Sunday
Undefined. Implement with the following snippet:
Given('today is Sunday', function () {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
? When I ask whether it's Friday yet
Undefined. Implement with the following snippet:
When('I ask whether it\'s Friday yet', function () {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
? Then I should be told "Nope"
Undefined. Implement with the following snippet:
Then('I should be told {string}', function (string) {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
1 Scenario (1 undefined)
3 steps (3 undefined)
0m00.000s
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday is not Friday # features/is_it_friday_yet.feature:4
Given today is Sunday # features/is_it_friday_yet.feature:5
When I ask whether it's Friday yet # features/is_it_friday_yet.feature:6
Then I should be told "Nope" # features/is_it_friday_yet.feature:7
1 scenario (1 undefined)
3 steps (3 undefined)
0m0.052s
You can implement step definitions for undefined steps with these snippets:
Given("today is Sunday") do
pending # Write code here that turns the phrase above into concrete actions
end
When("I ask whether it's Friday yet") do
pending # Write code here that turns the phrase above into concrete actions
end
Then("I should be told {string}") do |string|
pending # Write code here that turns the phrase above into concrete actions
end
未定義ステップのスニペットを 3 つすべてコピーして、src/test/java/hellocucumber/StepDefinitions.java
src/test/kotlin/hellocucumber/Stepdefs.kt
features/step_definitions/stepdefs.js
features/step_definitions/stepdefs.rb
に貼り付けます。
残念ながら、Cucumber は Kotlin でスニペットを生成しません。ですが、Kotlin コードへの Java コードの変換は IDEA で可能です。翻訳されたコードを改善して、より慣用的なものにする必要があります。また、次のインポート ステートメントを追加する必要がある場合もあります(まだ追加されていない場合)。
StepDefs.kt
ファイルは以下のように表示されるようになります
package hellocucumber
import io.cucumber.java.PendingException
import io.cucumber.java.en.Given
import io.cucumber.java.en.When
import io.cucumber.java.en.Then
import org.junit.Assert.*
class StepDefs {
@Given("today is Sunday")
@Throws(Exception::class)
fun today_is_Sunday() {
// Write code here that turns the phrase above into concrete actions
throw PendingException()
}
@When("I ask whether it's Friday yet")
@Throws(Exception::class)
fun i_ask_whether_it_s_Friday_yet() {
// Write code here that turns the phrase above into concrete actions
throw PendingException()
}
@Then("I should be told {string}")
@Throws(Exception::class)
fun i_should_be_told(arg1: String) {
// Write code here that turns the phrase above into concrete actions
throw PendingException()
}
}
Cucumber をもう一度実行します。この場合、出力が少し異なります
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
io.cucumber.java.PendingException: TODO: implement me
at hellocucumber.Stepdefs.today_is_Sunday(StepDefinitions.java:14)
at ?.today is Sunday(classpath:hellocucumber/is_it_friday_yet.feature:5)
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
Pending scenarios:
hellocucumber/is_it_friday_yet.feature:4 # Sunday isn't Friday
1 Scenarios (1 pending)
3 Steps (2 skipped, 1 pending)
0m0.188s
io.cucumber.java.PendingException: TODO: implement me
at hellocucumber.Stepdefs.today_is_Sunday(StepDefinitions.java:13)
at ?.today is Sunday(classpath:hellocucumber/is_it_friday_yet.feature:5)
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # StepDefs.today_is_Sunday()
io.cucumber.java.PendingException: TODO: implement me
at hellocucumber.StepDefs.today_is_Sunday(StepDefs.kt:14)
at ✽.today is Sunday(hellocucumber/is_it_friday_yet.feature:5)
When I ask whether it's Friday yet # StepDefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # StepDefs.i_should_be_told(String)
1 Scenarios (1 pending)
3 Steps (2 skipped, 1 pending)
0m0.107s
io.cucumber.java.PendingException: TODO: implement me
at hellocucumber.StepDefs.today_is_Sunday(StepDefs.kt:14)
at ✽.today is Sunday(hellocucumber/is_it_friday_yet.feature:5)
Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.351 sec
P--
Warnings:
1) Scenario: Sunday is not Friday # features/is_it_friday_yet.feature:4
? Given today is Sunday # features/step_definitions/stepdefs.js:3
Pending
- When I ask whether it's Friday yet # features/step_definitions/stepdefs.js:8
- Then I should be told "Nope" # features/step_definitions/stepdefs.js:13
1 Scenario (1 pending)
3 steps (1 pending, 2 skipped)
0m00.001s
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday is not Friday # features/is_it_friday_yet.feature:4
Given today is Sunday # features/step_definitions/stepdefs.rb:1
TODO (Cucumber::Pending)
./features/step_definitions/stepdefs.rb:2:in `"today is Sunday"'
features/is_it_friday_yet.feature:5:in `Given today is Sunday'
When I ask whether it's Friday yet # features/step_definitions/stepdefs.rb:5
Then I should be told "Nope" # features/step_definitions/stepdefs.rb:9
1 scenario (1 pending)
3 steps (2 skipped, 1 pending)
0m0.073s
Cucumber はステップ定義を見つけ、これらを実行しました。現時点では処理中としてマークされています。つまり、役立つ動作をするようにする必要があるということです。
次のステップは、ステップ定義内のコメントで行う必要があることの実行です
上記の文章を具体的なアクションに変換するコードをここに記述します
ステップと同じ単語をコードに使用してみてください。
手順の定義コードを以下に変更します。
package hellocucumber;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import static org.junit.jupiter.api.Assertions.*;
class IsItFriday {
static String isItFriday(String today) {
return null;
}
}
public class Stepdefs {
private String today;
private String actualAnswer;
@Given("today is Sunday")
public void today_is_Sunday() {
today = "Sunday";
}
@When("I ask whether it's Friday yet")
public void i_ask_whether_it_s_Friday_yet() {
actualAnswer = IsItFriday.isItFriday(today);
}
@Then("I should be told {string}")
public void i_should_be_told(String expectedAnswer) {
assertEquals(expectedAnswer, actualAnswer);
}
}
package hellocucumber
import io.cucumber.java.en.Then
import io.cucumber.java.en.Given
import io.cucumber.java.en.When
import junit.framework.Assert.assertEquals
fun isItFriday(today: String) = ""
class StepDefs {
private lateinit var today: String
private lateinit var actualAnswer: String
@Given("today is Sunday")
fun today_is_Sunday() {
today = "Sunday"
}
@When("I ask whether it's Friday yet")
fun i_ask_whether_it_s_Friday_yet() {
actualAnswer = isItFriday(today)
}
@Then("I should be told {string}")
fun i_should_be_told(expectedAnswer: String) {
assertEquals(expectedAnswer, actualAnswer)
}
}
const assert = require('assert');
const { Given, When, Then } = require('@cucumber/cucumber');
function isItFriday(today) {
// We'll leave the implementation blank for now
}
Given('today is Sunday', function () {
this.today = 'Sunday';
});
When('I ask whether it\'s Friday yet', function () {
this.actualAnswer = isItFriday(this.today);
});
Then('I should be told {string}', function (expectedAnswer) {
assert.strictEqual(this.actualAnswer, expectedAnswer);
});
module FridayStepHelper
def is_it_friday(day)
end
end
World FridayStepHelper
Given("today is Sunday") do
@today = 'Sunday'
end
When("I ask whether it's Friday yet") do
@actual_answer = is_it_friday(@today)
end
Then("I should be told {string}") do |expected_answer|
expect(@actual_answer).to eq(expected_answer)
end
Cucumberをもう一度実行します。
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
java.lang.AssertionError: expected:<Nope> but was:<null>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at hellocucumber.Stepdefs.i_should_be_told(StepDefinitions.java:31)
at ?.I should be told "Nope"(classpath:hellocucumber/is_it_friday_yet.feature:7)
Failed scenarios:
hellocucumber/is_it_friday_yet.feature:4 # Sunday isn't Friday
1 Scenarios (1 failed)
3 Steps (1 failed, 2 passed)
0m0.404s
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # StepDefs.today_is_Sunday()
When I ask whether it's Friday yet # StepDefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # StepDefs.i_should_be_told(String)
junit.framework.ComparisonFailure: expected:<[Nope]> but was:<[]>
at junit.framework.Assert.assertEquals(Assert.java:100)
at junit.framework.Assert.assertEquals(Assert.java:107)
at hellocucumber.StepDefs.i_should_be_told(StepDefs.kt:30)
at ✽.I should be told "Nope"(hellocucumber/is_it_friday_yet.feature:7)
..F
Failures:
1) Scenario: Sunday is not Friday # features/is_it_friday_yet.feature:4
✔ Given today is Sunday # features/step_definitions/stepdefs.js:8
✔ When I ask whether it's Friday yet # features/step_definitions/stepdefs.js:12
✖ Then I should be told "Nope" # features/step_definitions/stepdefs.js:16
AssertionError [ERR_ASSERTION]: undefined == 'Nope'
at World.<anonymous> (/private/tmp/tutorial/hellocucumber/features/step_definitions/stepdefs.js:17:10)
1 Scenario (1 failed)
3 steps (1 failed, 2 passed)
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday is not Friday # features/is_it_friday_yet.feature:4
Given today is Sunday # features/step_definitions/stepdefs.rb:4
When I ask whether it's Friday yet # features/step_definitions/stepdefs.rb:8
Then I should be told "Nope" # features/step_definitions/stepdefs.rb:12
expected: "Nope"
got: nil
(compared using ==)
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/stepdefs.rb:13:in `"I should be told {string}"'
features/is_it_friday_yet.feature:7:in `Then I should be told "Nope"'
Failing Scenarios:
cucumber features/is_it_friday_yet.feature:4 # Scenario: Sunday is not Friday
1 scenario (1 failed)
3 steps (1 failed, 2 passed)
0m0.092s
進捗中です。最初の2つの手順はパスしていますが、最後の手順は失敗しています。
シナリオをパスにするために最低限必要な処理を行います。この場合は、メソッド
関数ブロック関数関数がNope
を返すようにする必要があります。
static String isItFriday(String today) {
return "Nope";
}
fun isItFriday(today: String) = "Nope"
function isItFriday(today) {
return 'Nope';
}
def is_it_friday(day)
'Nope'
end
Cucumberをもう一度実行します。
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
1 Scenarios (1 passed)
3 Steps (3 passed)
0m0.255s
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
1 Scenarios (1 passed)
3 Steps (3 passed)
0m0.255s
...
1 Scenario (1 passed)
3 steps (3 passed)
0m00.003s
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday is not Friday # features/is_it_friday_yet.feature:4
Given today is Sunday # features/step_definitions/stepdefs.rb:5
When I ask whether it's Friday yet # features/step_definitions/stepdefs.rb:9
Then I should be told "Nope" # features/step_definitions/stepdefs.rb:13
1 scenario (1 passed)
3 steps (3 passed)
0m0.066s
おめでとうございます。最初の緑色Cucumberシナリオができました。
次にテストする必要があるのは、それが金曜日のときにも正しい結果を取得できるかどうかです。
is_it_friday_yet.feature
ファイルを更新します。
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday
Given today is Sunday
When I ask whether it's Friday yet
Then I should be told "Nope"
Scenario: Friday is Friday
Given today is Friday
When I ask whether it's Friday yet
Then I should be told "TGIF"
today
を「Friday」に設定するための手順定義を追加する必要があります。
@Given("today is Friday")
public void today_is_Friday() {
today = "Friday";
}
@Given("today is Friday")
fun today_is_Friday() {
today = "Friday"
}
Given('today is Friday', function () {
this.today = 'Friday';
});
Given("today is Friday") do
@today = 'Friday'
end
このテストを実行すると、失敗します。
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
Scenario: Friday is Friday # hellocucumber/is_it_friday_yet.feature:9
Given today is Friday # Stepdefs.today_is_Friday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "TGIF" # Stepdefs.i_should_be_told(String)
org.junit.ComparisonFailure: expected:<[TGIF]> but was:<[Nope]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at hellocucumber.Stepdefs.i_should_be_told(StepDefinitions.java:36)
at ?.I should be told "TGIF"(classpath:hellocucumber/is_it_friday_yet.feature:12)
Failed scenarios:
hellocucumber/is_it_friday_yet.feature:9 # Friday is Friday
2 Scenarios (1 failed, 1 passed)
6 Steps (1 failed, 5 passed)
0m0.085s
org.junit.ComparisonFailure: expected:<[TGIF]> but was:<[Nope]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at hellocucumber.Stepdefs.i_should_be_told(StepDefinitions.java:36)
at ?.I should be told "TGIF"(classpath:hellocucumber/is_it_friday_yet.feature:12)
.....F
Failures:
1) Scenario: Friday is Friday # features/is_it_friday_yet.feature:9
✔ Given today is Friday # features/step_definitions/stepdefs.js:8
✔ When I ask whether it's Friday yet # features/step_definitions/stepdefs.js:16
✖ Then I should be told "TGIF" # features/step_definitions/stepdefs.js:20
AssertionError [ERR_ASSERTION]: 'Nope' == 'TGIF'
+ expected - actual
-Nope
+TGIF
at World.<anonymous> (/private/tmp/tutorial/hellocucumber/features/step_definitions/stepdefs.js:21:10)
2 scenarios (1 failed, 1 passed)
6 steps (1 failed, 5 passed)
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # features/is_it_friday_yet.feature:4
Given today is Sunday # features/step_definitions/stepdefs.rb:12
When I ask whether it's Friday yet # features/step_definitions/stepdefs.rb:16
Then I should be told "Nope" # features/step_definitions/stepdefs.rb:20
Scenario: Friday is Friday # features/is_it_friday_yet.feature:9
Given today is Friday # features/step_definitions/stepdefs.rb:8
When I ask whether it's Friday yet # features/step_definitions/stepdefs.rb:16
Then I should be told "TGIF" # features/step_definitions/stepdefs.rb:20
expected: "TGIF"
got: "Nope"
(compared using ==)
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/stepdefs.rb:21:in `"I should be told {string}"'
features/is_it_friday_yet.feature:12:in `Then I should be told "TGIF"'
Failing Scenarios:
cucumber features/is_it_friday_yet.feature:9 # Scenario: Friday is Friday
2 scenarios (1 failed, 1 passed)
6 steps (1 failed, 5 passed)
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/isitfriday.feature:4
Given today is Sunday # StepDefs.today_is_Sunday()
When I ask whether it's Friday yet # StepDefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # StepDefs.i_should_be_told(String)
Scenario: Friday is Friday # hellocucumber/isitfriday.feature:9
Given today is Friday # StepDefs.today_is_Friday()
When I ask whether it's Friday yet # StepDefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "TGIF" # StepDefs.i_should_be_told(String)
org.junit.ComparisonFailure: expected:<[TGIF]> but was:<[Nope]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at hellocucumber.StepDefs.i_should_be_told(StepDefs.kt:40)
at ✽.I should be told "TGIF"(hellocucumber/isitfriday.feature:12)
Failed scenarios:
hellocucumber/isitfriday.feature:9 # Friday is Friday
2 Scenarios (1 failed, 1 passed)
6 Steps (1 failed, 5 passed)
0m0.100s
org.junit.ComparisonFailure: expected:<[TGIF]> but was:<[Nope]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at hellocucumber.StepDefs.i_should_be_told(StepDefs.kt:40)
at ✽.I should be told "TGIF"(hellocucumber/isitfriday.feature:12)
これは、論理がまだ実装されていないためです。次にそれを行います。
today
が"Friday"
と等しいかどうかを実際に評価するために、ステートメントを更新する必要があります。
static String isItFriday(String today) {
return "Friday".equals(today) ? "TGIF" : "Nope";
}
fun isItFriday(today: String) = if (today == "Friday") "TGIF" else "Nope"
function isItFriday(today) {
if (today === "Friday") {
return "TGIF";
} else {
return "Nope";
}
}
def is_it_friday(day)
if day == 'Friday'
'TGIF'
else
'Nope'
end
end
Cucumberをもう一度実行します。
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
Scenario: Friday is Friday # hellocucumber/is_it_friday_yet.feature:9
Given today is Friday # Stepdefs.today_is_Friday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "TGIF" # Stepdefs.i_should_be_told(String)
2 Scenarios (2 passed)
6 Steps (6 passed)
0m0.255s
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is Sunday # Stepdefs.today_is_Sunday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
Scenario: Friday is Friday # hellocucumber/is_it_friday_yet.feature:9
Given today is Friday # Stepdefs.today_is_Friday()
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "TGIF" # Stepdefs.i_should_be_told(String)
2 Scenarios (2 passed)
6 Steps (6 passed)
0m0.255s
......
2 scenarios (2 passed)
6 steps (6 passed)
0m00.002s
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # features/is_it_friday_yet.feature:4
Given today is Sunday # features/step_definitions/stepdefs.rb:8
When I ask whether it's Friday yet # features/step_definitions/stepdefs.rb:17
Then I should be told "Nope" # features/step_definitions/stepdefs.rb:22
Scenario: Friday is Friday # features/is_it_friday_yet.feature:9
Given today is Friday # features/step_definitions/stepdefs.rb:12
When I ask whether it's Friday yet # features/step_definitions/stepdefs.rb:17
Then I should be told "TGIF" # features/step_definitions/stepdefs.rb:22
2 scenarios (2 passed)
6 steps (6 passed)
0m0.040s
では、日曜日と金曜日の他にも週には多くの曜日があることを皆さんはご存知でしょう。複数のExamples
を使い始めるとき、シナリオを更新して変数を使用し、より多くの可能性を評価しましょう。変数と例を使用して、金曜日、日曜日、その他すべてを評価します。
is_it_friday_yet.feature
ファイルを更新します。複数のExamples
を使用すると、Scenario
からScenario Outline
に変化することがわかります。
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario Outline: Today is or is not Friday
Given today is "<day>"
When I ask whether it's Friday yet
Then I should be told "<answer>"
Examples:
| day | answer |
| Friday | TGIF |
| Sunday | Nope |
| anything else! | Nope |
today is Sunday
とtoday is Friday
の手順定義を、文字列として<day>
の値を取る1つの手順定義に置き換える必要があります。StepDefinitions.java
stepdefs.js
stepdefs.rb
ファイルを以下のように更新します。
package hellocucumber;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import static org.junit.jupiter.api.Assertions.*;
class IsItFriday {
static String isItFriday(String today) {
return "Friday".equals(today) ? "TGIF" : "Nope";
}
}
public class Stepdefs {
private String today;
private String actualAnswer;
@Given("today is {string}")
public void today_is(String today) {
this.today = today;
}
@When("I ask whether it's Friday yet")
public void i_ask_whether_it_s_Friday_yet() {
actualAnswer = IsItFriday.isItFriday(today);
}
@Then("I should be told {string}")
public void i_should_be_told(String expectedAnswer) {
assertEquals(expectedAnswer, actualAnswer);
}
}
package hellocucumber
import io.cucumber.java.en.Then
import io.cucumber.java.en.Given
import io.cucumber.java.en.When
import static org.junit.jupiter.api.Assertions.assertEquals
fun isItFriday(today: String) = if (today == "Friday") "TGIF" else "Nope"
class StepDefs {
private lateinit var today: String
private lateinit var actualAnswer: String
@Given("today is {string}")
fun today_is(today: String) {
this.today = today
}
@When("I ask whether it's Friday yet")
fun i_ask_whether_it_s_Friday_yet() {
actualAnswer = isItFriday(today)
}
@Then("I should be told {string}")
fun i_should_be_told(expectedAnswer: String) {
assertEquals(expectedAnswer, actualAnswer)
}
}
const assert = require('assert');
const { Given, When, Then } = require('@cucumber/cucumber');
function isItFriday(today) {
if (today === "Friday") {
return "TGIF";
} else {
return "Nope";
}
}
Given('today is {string}', function (givenDay) {
this.today = givenDay;
});
When('I ask whether it\'s Friday yet', function () {
this.actualAnswer = isItFriday(this.today);
});
Then('I should be told {string}', function (expectedAnswer) {
assert.strictEqual(this.actualAnswer, expectedAnswer);
});
module FridayStepHelper
def is_it_friday(day)
if day == 'Friday'
'TGIF'
else
'Nope'
end
end
end
World FridayStepHelper
Given("today is {string}") do |given_day|
@today = given_day
end
When("I ask whether it's Friday yet") do
@actual_answer = is_it_friday(@today)
end
Then("I should be told {string}") do |expected_answer|
expect(@actual_answer).to eq(expected_answer)
end
Cucumberをもう一度実行します。
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is "<day>"
When I ask whether it's Friday yet
Then I should be told "<answer>"
Examples:
Scenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:11
Given today is "Friday" # Stepdefs.today_is(String)
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "TGIF" # Stepdefs.i_should_be_told(String)
Scenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:12
Given today is "Sunday" # Stepdefs.today_is(String)
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
Scenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:13
Given today is "anything else!" # Stepdefs.today_is(String)
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
3 Scenarios (3 passed)
9 Steps (9 passed)
0m0.255s
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:4
Given today is "<day>"
When I ask whether it's Friday yet
Then I should be told "<answer>"
Examples:
Scenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:11
Given today is "Friday" # Stepdefs.today_is(String)
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "TGIF" # Stepdefs.i_should_be_told(String)
Scenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:12
Given today is "Sunday" # Stepdefs.today_is(String)
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
Scenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:13
Given today is "anything else!" # Stepdefs.today_is(String)
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "Nope" # Stepdefs.i_should_be_told(String)
3 Scenarios (3 passed)
9 Steps (9 passed)
0m0.255s
.........
3 scenarios (3 passed)
9 steps (9 passed)
0m00.001s
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario Outline: Today is or is not Friday # features/is_it_friday_yet.feature:4
Given today is <day> # features/is_it_friday_yet.feature:5
When I ask whether it's Friday yet # features/is_it_friday_yet.feature:6
Then I should be told <answer> # features/is_it_friday_yet.feature:7
Examples:
| day | answer |
| "Friday" | "TGIF" |
| "Sunday" | "Nope" |
| "anything else!" | "Nope" |
3 scenarios (3 passed)
9 steps (9 passed)
0m0.021s
動作するコードができましたので、いくつかのリファクタリングを実施する必要があります。
isItFriday
メソッド関数ブロック関数関数をテストコードから本番コードに移動する必要があります。
今後、いくつかの場所で使用する手順定義からヘルパーメソッドを抽出できます。メソッド関数関数ブロックの場合。
この簡単なチュートリアルでは、Cucumberのインストール方法、BDDプロセスに従ってメソッド関数ブロック関数関数を開発する方法、およびそのメソッド関数ブロック関数関数を使用して複数のシナリオを評価する方法を学習しました。
このドキュメントの改善にご協力ください。このページを編集。