範例對應
嘗試在您的團隊中執行 範例對應 工作坊,以一起設計範例。
在這個快速教學中,您將學習如何
我們將使用 Cucumber 開發一個小型程式庫,該程式庫可以判斷是否是星期五。
請注意,本教學假設您具備
Gemfile
的基本理解在開始之前,您需要以下項目
我們將從使用 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
目錄並選取 「將目錄標記為」>「測試來源根」 來執行此操作。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
目錄並選取 「將目錄標記為」>「測試來源根」 來執行此操作。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 中,一個範例稱為情境 (scenario)。情境定義在 .feature
檔案中,這些檔案儲存在 src/test/resources/hellocucumber
features
features
目錄(或其子目錄)中。
一個具體的例子是:星期天不是星期五。
建立一個名為 src/test/resources/hellocucumber/is_it_friday_yet.feature
src/test/resources/hellocucumber/is_it_friday_yet.feature
features/is_it_friday_yet.feature
features/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:
開頭,後接名稱。最好使用與檔案名稱相似的名稱。
第二行是功能的簡短描述。 Cucumber 不會執行此行,因為它是文件。
第四行,Scenario: Sunday is not Friday
是一個情境,它是一個具體的範例,說明軟體應如何運作。
最後三行以 Given
、When
和 Then
開頭,是我們情境的步驟。這是 Cucumber 將會執行的部分。
現在我們有了一個情境,我們可以要求 Cucumber 執行它。
mvn test
mvn test
npm test
cucumber
Cucumber 告訴我們有一個 undefined
的情境和三個 undefined
的步驟。它還建議了一些程式碼片段,我們可以利用這些程式碼片段來定義這些步驟。
-------------------------------------------------------
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.dev.org.tw/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.dev.org.tw/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
複製每個未定義步驟的三個程式碼片段,並將它們貼到 src/test/java/hellocucumber/StepDefinitions.java
src/test/kotlin/hellocucumber/Stepdefs.kt
features/step_definitions/stepdefs.js
features/step_definitions/stepdefs.rb
中。
不幸的是,Cucumber 沒有產生 Kotlin 的程式碼片段。但幸運的是,IDEA 可以將 Java 程式碼轉換為 Kotlin 程式碼。您可能需要改進轉換後的程式碼,使其更符合習慣用法。您可能還需要新增以下 import 語句(如果您尚未新增)。
您的 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
這是一個進展!前兩個步驟通過了,但最後一個步驟失敗了。
讓我們執行讓情境通過所需的最低限度操作。在這種情況下,這表示讓我們的方法函式區塊函式函式傳回 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
因此,我們都知道一週中的天數不只有星期天和星期五。讓我們更新情境以使用變數,並評估更多可能性。我們將使用變數和範例來評估星期五、星期天和任何其他日期!
更新 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>
的值作為字串的步驟定義。如下更新 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 流程來開發方法函式區塊函式函式,以及如何使用該方法函式區塊函式函式來評估多個情境!
您可以幫助我們改進此文件。編輯此頁面。