Example Mapping
Try running an Example Mapping workshop in your team to design examples together.
In this quick tutorial you will learn how to:
We’ll use Cucumber to develop a small library that can figure out whether it’s Friday yet.
Please be aware that this tutorial assumes that you have a:
Gemfile
Before we begin, you will need the following:
Open a terminal to verify that Node.js is installed properly:
node -v
npm -v
Both of these commands should print a version number.
Open a terminal to verify that Ruby is installed properly:
ruby -v
bundle -v
Both of these commands should print a version number.
We’ll start by creating a new project directory with the cucumber-archetype
Maven plugin. Open a terminal, go to the directory where you want to create your project,
and run the following command:
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"
You should get something like the following result:
[INFO] Project created from Archetype in dir: <directory where you created the project>/cucumber
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Change into the directory that was just created by running the following command:
cd hellocucumber
Open the project in IntelliJ IDEA:
We’ll start by creating a new project directory with the cucumber-archetype
Maven plugin. Open a terminal, go to the directory where you want to create your project,
and run the following command:
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"
You should get something like the following result:
[INFO] Project created from Archetype in dir: <directory where you created the project>/cucumber
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Change into the directory that was just created by running the following command:
cd hellocucumber
Open the project in IntelliJ IDEA:
To use Kotlin, we need to add it to our project:
kotlin
in your src/test
directory and mark it as Test Sources Root
.
In IntelliJ IDEA, you can do so by right-clicking on the kotlin
directory and selecting “Mark Directory as” > “Test Sources Root”.hellocucumber
package inside the kotlin
directory.RunCucumberTest
inside the hellocucumber
package. IntelliJ IDEA might tell you that Kotlin is not configured; click “Configure”.
Your pom.xml
should now look like this:<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.8.10</kotlin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-bom</artifactId>
<version>7.11.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<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.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<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.11.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>
</configuration>
</plugin>
</plugins>
</build>
</project>
RunCucumberTest.java
class to the RunCucumberTest.kt
class.
If you are using IntelliJ IDEA, it will offer to translate the Java code to Kotlin code. Otherwise, you’ll have to write your own.Your RunCucumberTest.kt
class should now look like this:
package hellocucumber
import io.cucumber.junit.platform.engine.Constants
import org.junit.platform.suite.api.ConfigurationParameter
import org.junit.platform.suite.api.IncludeEngines
import org.junit.platform.suite.api.SelectClasspathResource
import org.junit.platform.suite.api.Suite
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("hellocucumber")
@ConfigurationParameter(key = Constants.PLUGIN_PROPERTY_NAME, value = "pretty")
class RunCucumberTest
RunCucumberTest.java
class.StepDefs
inside the hellocucumber
package.StepDefinitions.java
to StepDefs.kt
; you’ll need them later.StepDefinitions.java
class (or even the java
directory).We’ll start by creating a new directory and an empty Node.js project.
mkdir hellocucumber
cd hellocucumber
npm init --yes
Add Cucumber as a development dependency:
npm install --save-dev @cucumber/cucumber
Open package.json
in a text editor and change the test
section so it looks like this:
{
"name": "hellocucumber",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "cucumber-js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"cucumber": "^11.1.0"
}
}
Prepare the file structure:
mkdir features
mkdir features/step_definitions
Create a file called cucumber.js
at the root of your project and add the following
content:
module.exports = {
default: `--format-options '{"snippetInterface": "synchronous"}'`
}
Also, create a file called features/step_definitions/stepdefs.js
with the
following content:
const assert = require('assert');
const { Given, When, Then } = require('@cucumber/cucumber');
We’ll start by creating a new directory and an empty Ruby project.
mkdir hellocucumber
cd hellocucumber
Create a Gemfile
with the following content:
source "https://rubygems.org"
group :test do
gem 'cucumber', '~> 9.2.0'
gem 'rspec', '~> 3.13.0'
end
Install Cucumber and prepare the file structure:
bundle install
cucumber --init
You now have a small project with Cucumber installed.
To make sure everything works together correctly, let’s run Cucumber.
mvn test
mvn test
# Run via NPM
npm test
# Run standalone
npx cucumber-js
cucumber
You should see something like the following:
-------------------------------------------------------
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’s output is telling us that it didn’t find anything to run.
When we do Behaviour-Driven Development with Cucumber we use concrete examples to specify what we want the software to do. Scenarios are written before production code. They start their life as an executable specification. As the production code emerges, scenarios take on a role as living documentation and automated tests.
In Cucumber, an example is called a scenario.
Scenarios are defined in .feature
files, which are stored in the
src/test/resources/hellocucumber
features
features
directory (or a subdirectory).
One concrete example would be that Sunday isn’t Friday.
Create an empty file called
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
with the following content:
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"
The first line of this file starts with the keyword Feature:
followed by a name.
It’s a good idea to use a name similar to the file name.
The second line is a brief description of the feature. Cucumber does not execute this line because it’s documentation.
The fourth line, Scenario: Sunday is not Friday
is a
scenario, which is a concrete example illustrating how
the software should behave.
The last three lines starting with Given
, When
and Then
are the
steps of our scenario. This is what Cucumber will execute.
Now that we have a scenario, we can ask Cucumber to execute it.
mvn test
mvn test
npm test
cucumber
Cucumber is telling us we have one undefined
scenario and three undefined
steps. It’s also suggesting some snippets of code that we can use to
define these steps:
-------------------------------------------------------
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.io/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.io/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
Copy each of the three snippets for the undefined steps and paste them into
src/test/java/hellocucumber/StepDefinitions.java
src/test/kotlin/hellocucumber/Stepdefs.kt
features/step_definitions/stepdefs.js
features/step_definitions/stepdefs.rb
.
Unfortunately, Cucumber does not generate snippets in Kotlin. But fortunately IDEA can convert the Java code to Kotlin code for you. You might need to improve the translated code, to make it more idiomatic. You might also need to add the following import statements (if you hadn’t already).
Your StepDefs.kt
file should now look like this:
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()
}
}
Run Cucumber again. This time the output is a little different:
-------------------------------------------------------
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 found our step definitions and executed them. They are currently marked as pending, which means we need to make them do something useful.
The next step is to do what the comments in the step definitions is telling us to do:
Write code here that turns the phrase above into concrete actions
Try to use the same words in the code as in the steps.
Change your step definition code to this:
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
Run Cucumber again:
-------------------------------------------------------
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
That’s progress! The first two steps are passing, but the last one is failing.
Let’s do the minimum we need to make the scenario pass. In this case, that means making our methodfunctionblockfunctionfunction return 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
Run Cucumber again:
-------------------------------------------------------
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
Congratulations! You’ve got your first green Cucumber scenario.
The next thing to test for would be that we also get the correct result when it is Friday.
Update the is_it_friday_yet.feature
file:
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"
We’ll need to add a step definition to set today
to “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
When we run this test, it will fail.
-------------------------------------------------------
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)
That is because we haven’t implemented the logic yet! Let’s do that next.
We should update our statement to actually evaluate whether or not today
is equal to "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
Run Cucumber again:
-------------------------------------------------------
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
So, we all know that there are more days in the week than just Sunday and Friday. Let’s update our scenario to use variables and evaluate more possibilities. We’ll use variables and examples to evaluate Friday, Sunday, and anything else!
Update the is_it_friday_yet.feature
file. Notice how we go from Scenario
to Scenario Outline
when we start using multiple Examples
.
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 |
We need to replace the step definitions for today is Sunday
and today is Friday
with one step definition that takes the value of <day>
as a String.
Update the StepDefinitions.java
stepdefs.js
stepdefs.rb
file as follows:
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
Run Cucumber again:
-------------------------------------------------------
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
Now that we have working code, we should do some refactoring:
We should move the isItFriday
methodfunctionblockfunctionfunction out from the test code into production code.
We could at some point extract helper methods from our step definition, for methodsfunctionsfunctionsblocks we use in several places.
In this brief tutorial you’ve seen how to install Cucumber, how to follow the BDD process to develop a methodfunctionblockfunctionfunction, and how to use that methodfunctionblockfunctionfunction to evaluate multiple scenarios!
You can help us improve this documentation. Edit this page.