示例映射
尝试在您的团队中运行一个示例映射 研讨会,以共同设计示例。
在本快速教程中,您将学习如何
我们将使用 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.cn"
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
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.fullstack.org.cn/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.fullstack.org.cn/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 代码。您可能需要改进翻译后的代码,使其更符合惯例。您可能还需要添加以下导入语句(如果您还没有添加)。
您的 `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` 设置为“星期五”。
@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 流程来开发一个 方法函数块函数函数,以及如何使用该 方法函数块函数函数 来评估多个场景!
您可以帮助我们改进本文档。 编辑此页面.