Guide
BDD examples: from a Jira story to a passing test
By Miftode Vasilica, maker of Gherkin Studio ·
Behaviour-driven development (BDD) starts with a conversation about examples and ends with those examples running as tests. Most pages show only the middle step, a Gherkin scenario. This one follows a single story all the way: the story in Jira, the rules, the examples, the feature file, the code that runs it, a failing run, a passing run, and the result back on the story. We ran every file on this page with cucumber-js 13.3. After that come five shorter examples from other kinds of software.
Where BDD comes from
Dan North introduced the name in an article first published in 2006. He had found that the word "test" got in the way when teaching test-driven development, and that asking what code should do worked better. In a later article, "What's in a Story?" (2007), he wrote down the shape most teams still use: a story with a title and a narrative, and acceptance criteria written as scenarios with Given, When and Then. His own example was a bank customer taking cash from an ATM, with one scenario for an account that has enough money and one for an account that does not.
The Cucumber documentation describes the work as three practices:
- Discovery: talk through the story and find examples of what it should do.
- Formulation: write the examples down in a form people and machines can both read, usually Gherkin.
- Automation: connect each step to code, so the examples run as tests.
Step 1: the story
A story in Jira, SHOP-90:
Free delivery on larger orders. As a shopper, I want free delivery on larger orders, so that I can avoid paying for delivery.
It looks finished. It is not: it does not say what "larger" means.
Step 2: the conversation
The product owner, a developer and a tester spend ten minutes on it. The questions and answers:
- What counts as larger? $50 or more, before delivery.
- Exactly $50? Free. "Or more."
- What does delivery cost otherwise? $4.95.
- Do discount codes count towards the $50? Nobody knows. That one goes to the product owner as an open question.
Two examples come out of it, one on each side of the limit: a cart of $49.99 and a cart of $50.00. This kind of session is often called three amigos, and example mapping is a simple way to run it.
Step 3: the feature file
The examples, written in Gherkin. The story text stays as the description, and the @SHOP-90 tag ties every scenario to the Jira issue.
@SHOP-90
Feature: Free delivery
As a shopper
I want free delivery on larger orders
So that I can avoid paying for delivery
Rule: Orders of $50 or more are delivered free
Example: Just under the limit
Given my cart holds items worth $49.99
When I go to checkout
Then delivery costs $4.95
Example: Exactly at the limit
Given my cart holds items worth $50.00
When I go to checkout
Then delivery is free
Saved as features/free_delivery.feature. Run with nothing else in place, cucumber-js reports 2 scenarios (2 undefined) and prints a snippet for each of the four distinct steps. The step text has not been connected to any code yet. More on the file itself in Cucumber feature files.
Step 4: the step definitions
Each step needs a small function. The text in quotes must match the step; {float} picks up the number.
const { Given, When, Then } = require('@cucumber/cucumber');
const assert = require('node:assert');
const { deliveryCents } = require('../../src/delivery');
const cents = (dollars) => Math.round(dollars * 100);
Given('my cart holds items worth ${float}', function (dollars) {
this.cartCents = cents(dollars);
});
When('I go to checkout', function () {
this.deliveryCents = deliveryCents(this.cartCents);
});
Then('delivery costs ${float}', function (dollars) {
assert.strictEqual(this.deliveryCents, cents(dollars));
});
Then('delivery is free', function () {
assert.strictEqual(this.deliveryCents, 0);
});
Saved as delivery_steps.js, in the folder features/step_definitions/. The amounts are turned into whole cents, because decimal numbers in JavaScript are not exact: 0.1 + 0.2 gives 0.30000000000000004.
Step 5: a run that fails
The first version of the code, src/delivery.js, has the mistake that edges exist to catch: > where the rule says "or more".
const FREE_FROM_CENTS = 5000;
const DELIVERY_CENTS = 495;
function deliveryCents(cartCents) {
return cartCents > FREE_FROM_CENTS ? 0 : DELIVERY_CENTS;
}
module.exports = { deliveryCents };
npx cucumber-js prints, shortened:
Failed scenarios:
1) Exactly at the limit # features/free_delivery.feature:14
Then delivery is free # features/step_definitions/delivery_steps.js:19
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
495 !== 0
2 scenarios (1 passed, 1 failed)
6 steps (5 passed, 1 failed)
A $50.00 cart is still charged $4.95. A scenario with a cart of $60 would have passed, and so would one with $30. Only the example at the limit finds the bug.
Step 6: a run that passes
One character changes: > becomes >=.
return cartCents >= FREE_FROM_CENTS ? 0 : DELIVERY_CENTS;
2 scenarios (2 passed)
6 steps (6 passed)
Step 7: the result on the story
In CI, the same run writes a Cucumber JSON report:
npx cucumber-js --format json:cucumber.json
In that file, each scenario carries the tags it inherited from the feature, so both examples carry @SHOP-90. A tool that reads the report can put the result on the right story. The Cucumber JSON report explains the rest of the file.
We sent both reports, the failing one and the passing one, through Gherkin Studio's report reader. For SHOP-90 it gave 1/2 failing, with the failed step Then delivery is free, and then 2/2 passing. That is what the team sees on the story, without opening the CI log. How to send the report from a pipeline: show Cucumber test results on Jira stories.
Five shorter BDD examples
Each one is a rule from a different kind of software, with the examples at its edge. All of them pass the official Cucumber parser.
Booking. A table can be booked up to 2 hours before it is free.
Scenario: Booking a table 2 hours ahead
Given table 4 is free from 19:00
When I book table 4 for 19:00 at 17:00
Then my booking is confirmed
Scenario: Booking too late
Given table 4 is free from 19:00
When I book table 4 for 19:00 at 17:01
Then I see "Bookings close 2 hours before"
Banking. A transfer larger than the balance is refused, and nothing moves.
Scenario: Transferring more than the balance
Given my account holds $200
When I transfer $250 to Ana
Then I see "Not enough money for this transfer"
And my account still holds $200
Healthcare scheduling. A patient gets a reminder 24 hours before an appointment, unless the appointment was booked less than 24 hours ahead.
Scenario: Reminder for an appointment booked a week ahead
Given I booked an appointment for Friday at 10:00 a week ago
When it is Thursday at 10:00
Then I receive a reminder
Scenario: No reminder for a same-day booking
When I book an appointment for today at 16:00
Then I receive no reminder
An API. A request without a token gets a 401 and no data.
Scenario: Asking for orders without a token
When a client asks for "/orders" without a token
Then the response status is 401
And the response contains no orders
Permissions. Only the author or an admin can delete a comment.
Scenario Outline: Who can delete a comment
Given Bogdan wrote a comment
When <person> tries to delete it
Then the comment <result>
Examples:
| person | result |
| Bogdan | is deleted |
| an admin | is deleted |
| another member | is not deleted |
What makes these good examples
- Each one has a real value at an edge: 17:00 and 17:01, $200 and $250, $49.99 and $50.00.
- Each one checks something a person can see, not a row in a database.
- None of them mentions a button or a page. They will still be true after a redesign.
- The scenario name says what is different between it and its neighbour. Dan North recommends exactly that.
More habits, with before-and-after versions: how to write good Gherkin scenarios.
Short answers
What is an example of BDD?
A story such as "free delivery on larger orders", the examples the team agreed on ($49.99 pays delivery, $50.00 does not), those examples written in Gherkin, and code that runs them as tests.
What is a BDD scenario?
One example of how the software should behave, written as Given (the situation), When (the action) and Then (the result someone can check).
Do I need Cucumber to do BDD?
No. The conversation and the written examples are already BDD. Cucumber, behave, Reqnroll and similar tools add the automation.
Is BDD only for testers?
No. The examples are agreed by the product person, the developers and the testers together. Often the developer and the tester write the scenarios after the conversation, and the product person reads them.
What is the difference between BDD and TDD?
TDD starts from a unit test a developer writes. BDD starts from examples of behaviour the whole team agrees on, in the language of the business.
Where Gherkin Studio fits
Gherkin Studio is a BDD app for Jira Cloud, built on Atlassian Forge. It keeps the scenarios on the story and shows the result of each run there. It is on the Atlassian Marketplace, with a 30-day free trial. See also user stories in Gherkin.