Writing User Stories With Gherkin
Feb 02, 2022
As a PM, the benefits of using Gherkin are Gherkin
Originally intended for developers, Gherkin is a structured approach to writing behavioral tests, also called Behavior Driven Development (BDD). Instead of testing little bits of code, behavioral tests seek to follow true user workflow, such as signing in, or applying for a refund. This means a focus on how users interact with your system.
Gherkin is the perfect framework for writing user stories because it gives a consistent approach for reviewing all scenarios, defines the definition of Done, and provides crisp acceptance criteria.
Benefits of using Gherkin are:
- You’ll catch missing workflows before any work is started.
- It’s a direct correlation to the user workflows that you and Design have developed.
- Developers know when the story is Done because they have clear acceptance criteria. QA and PM too!
- A consistent language across stories helps the team focus on delivering user value, unhindered by your writing style that day.
Feature: As a user I want to sign in so I can see my marketing campaigns
Scenario: User supplies correct user name and password
Given that I am on the sign-in page
When I enter my user name and password correctly
And click ‘Sign In’
Then I am taken to the dashboard
Scenario: User does NOT supply correct user name and password
Given that I am on the sign-in page
When I enter my user name and password incorrectly
and click ‘Sign In’
Then I see an error message ‘Sorry, incorrect user name or password.”
What happened there?
Gherkin follows a very specific syntax:
Scenario -> Given -> When -> Then
- Scenarios: All the actions a user could take (including bad input)
- GIVEN: Sets the context. What page are we on and what state are we in? Is the user an admin? Signed-in? Has created a campaign?
- WHEN: What actions the user is performing. What event occurred?
- THEN: What should the system do in response? What is the expected outcome?
Feature: Payments with Visa Classic
As a Visa Classic Cardholder,
I want to use my Visa Card,
For paying my purchases.
Scenario: The Visa Cardholder use the Visa Classic for paying
Given I am a Visa Classic Cardholder
When I pay with my Visa Classic
Then My Visa card is accepted
Multiple conditions can be concatenated with the AND keyword. For instance:
Given I am a Miles&More Fidelity Card holder,
And I Am a Premium Bank Customer
When I transfer money to my account
Then I also receive 100 points on my Miles&More Card
Multiple scenarios for a single feature are allowed, they can be expressed in a more concisely format with Scenario Outlines and data tables, for instance:
# Two scenarios for the same feature
Feature: Bank account calculation
As A bank customer,
I want the balance of my account correctly calculated
To be certain of my financial sistuation
Scenario: 1) bank account in-flow calculation
Given the starting balance has 4000 Euro
When 3000 euros are added to the account
Then the final balance is 7000 euro
Scenario: 2) bank account out-flow calculation
Given the starting balance has 4000 Euro
When 3000 euros are transferred to another account
Then the final balance is 1000 euro
# Here we use a scenario outline to include the
# two previous scenarios into a single one
Scenario Outline: bank account cash flow calculations
Given the starting balance has euros
When is trasferred
Then I should have euros
| start | trasferred | left |
| 4000 | +3000 | 7000 |
| 4000 | -3000 | 1000 |