Guide
How to write good Gherkin scenarios: BDD test cases, before and after
By Miftode Vasilica, maker of Gherkin Studio ·
A good Gherkin scenario describes one behaviour, with real values, in the words of the business, and ends with a result someone can see. It reads the same after a redesign. Most bad scenarios fail one of those tests in a way that is easy to spot once you know where to look. Below: how a classic test case becomes a scenario, nine habits with a weak and a better version of each, a review checklist, and how many scenarios a story needs. Where a rule comes from the Cucumber documentation, we say so.
From a test case to a scenario
Many teams arrive at Gherkin with test cases already written. A typical one:
| Field | Value |
|---|---|
| ID | TC-114 |
| Title | Verify login lockout |
| Preconditions | User exists, account active |
| Steps | 1. Open login page. 2. Enter valid email. 3. Enter wrong password. 4. Click Sign in. 5. Repeat steps 3–4 four more times. |
| Expected result | Error message shown, account locked |
Copied line by line into Gherkin, it becomes:
Scenario: Verify login lockout
Given the user exists
And the account is active
When I open the login page
And I enter a valid email
And I enter a wrong password
And I click "Sign in"
And I repeat this four more times
Then an error message is shown
And the account is locked
Nine steps, no numbers, and "an error message" that could say anything. Written as a behaviour instead:
Scenario: The fifth wrong password locks the account
Given Ana has entered a wrong password 4 times in a row
When she enters a wrong password again
Then she sees "Too many attempts. Try again in 15 minutes"
And her account is locked for 15 minutes
The test case described a procedure. The scenario describes a rule, and the procedure moves into the code behind the steps. The mapping is usually:
| Test case field | In the scenario |
|---|---|
| Title | The scenario name, saying what is different about this case |
| Preconditions | Given steps, as a state, not as actions |
| Steps | Usually one When; the clicks go into step definitions |
| Expected result | Then steps, with the exact text or value |
| Test data | Real values in the steps, or an Examples table |
1. Describe what happens, not how
Cucumber's guide "Writing better Gherkin" recommends a declarative style: say what the person does and what they get, not which fields they type into. It gives a question to test any step: will this wording need to change if the implementation does?
Weak:
Scenario: Free article for a free subscriber
Given I am on "/login"
When I type "[email protected]" in the email field
And I type "secret" in the password field
And I press "Submit"
Then I see "Free article" on the home page
Better:
Scenario: A free subscriber sees free articles
Given Frieda has a free subscription
When Frieda signs in
Then she sees the free articles
The login form will change. The rule that free subscribers see free articles will not.
2. One behaviour per scenario
A scenario with two When steps usually tests two things. When the second one fails, nobody can tell from the name what broke.
Weak:
Scenario: Cart
When I add a teapot to my cart
Then my cart holds 1 item
When I remove the teapot
Then my cart is empty
Better: two scenarios, and the first action becomes the starting state of the second.
Scenario: Adding an item
When I add a teapot to my cart
Then my cart holds 1 item
Scenario: Removing the only item
Given my cart holds a teapot
When I remove the teapot
Then my cart is empty
3. Keep it to three to five steps
The Gherkin reference recommends 3 to 5 steps per example. You can have more; the scenario just stops being readable as a specification. When a scenario grows past five, look for clicks that should be one step (habit 1) or a second behaviour (habit 2).
4. Given is a state, not a series of actions
The Gherkin reference describes Given as the initial context, usually something that happened in the past, and advises against user interaction in it.
| Weak | Better |
|---|---|
| Given I open the shop, And I search for "teapot", And I add it to my cart | Given my cart holds a teapot |
| Given I log in as admin | Given I am signed in as an admin |
| Given I create an order, And I pay for it | Given I have a paid order |
The test code can set up the state however it likes, faster than clicking through screens.
5. Then checks something you can see
The reference says the outcome in Then should be observable: something a user or another system receives. It warns against checking internal state, such as a database record.
| Weak | Better |
|---|---|
| Then the orders table has status 3 | Then the order shows "Cancelled" |
| Then the email queue has 1 message | Then Ana receives an email "Your order is on its way" |
| Then it works | Then I see my order history |
6. Use real values, at the edge
"A valid amount" starts no conversation. "$50.00" does, especially next to "$49.99". Two examples on either side of a limit find more bugs than ten in the middle. We show one found exactly this way in BDD examples, from a Jira story to a passing test.
Scenario Outline: Free delivery from $50
Given my cart holds items worth <amount>
When I go to checkout
Then delivery costs <delivery>
Examples:
| amount | delivery |
| $49.99 | $4.95 |
| $50.00 | $0.00 |
7. Name the scenario after what is different
Dan North's advice, from his 2007 article "What's in a Story?": a scenario's title should say what is different about it, so you can read a list of titles and see how the cases differ.
| Weak | Better |
|---|---|
| Withdraw cash 1 | Withdrawing with enough money in the account |
| Withdraw cash 2 | Withdrawing more than the balance |
| Test negative case | Withdrawing with a blocked card |
8. Do not reuse the same words for Given and Then
Cucumber does not look at the keyword when it matches a step to code. Given my account has a balance of $100 and Then my account has a balance of $70 match the same step definition. If you write one definition for each, cucumber-js refuses to choose. We ran it:
1 scenario (1 ambiguous)
3 steps (1 skipped, 2 ambiguous)
Give the check its own words:
Scenario: Withdrawing some cash
Given my account has a balance of $100
When I withdraw $30
Then my balance is now $70
That one passes. The same rule makes steps easier to reuse across a team; see Gherkin steps your team can reuse.
9. Pick one voice and keep it
"I", a named person ("Ana") or a role ("the admin") all work. Cucumber's own declarative example uses a named persona. What hurts is mixing them in one file, so readers wonder whether "I" and "the user" are the same person. Named personas help when a scenario has two people in it: "Ana shares the folder with Bogdan" is clearer than "I share the folder with the user".
A review checklist
Read each scenario against these questions before it goes into the sprint.
- Does the name say what is different about this case?
- Is there exactly one
When? - Are there five steps or fewer?
- Are the
Givensteps a state, not a list of clicks? - Does every
Thencheck something a person or another system can see? - Is there at least one real value, and is it near a limit?
- Would the wording survive a redesign of the screen?
- Would a product person understand every word?
- Does any
Thenreuse the exact words of aGiven? - Is the scenario tagged with the story it belongs to, such as
@SHOP-41? See linking scenarios to Jira issues.
How many scenarios does a story need?
Fewer than most teams write. For each rule of the story:
- One scenario on each side of the limit, if the rule has one.
- One scenario, if it has no limit.
- None, if the rule is obvious and nobody would argue about it.
When several scenarios differ only in values, use one Scenario Outline with an Examples table, as in habit 6. More in Scenario Outline and Examples.
If the count passes about six for one story, the story is probably two stories. Each rule is a natural place to split it.
Short answers
What is a Gherkin test case?
A test written in Gherkin: a scenario with Given, When and Then steps that a tool such as Cucumber runs. It describes one behaviour instead of a list of clicks.
What is the difference between a test case and a BDD scenario?
A test case usually lists steps and an expected result for a tester to follow. A scenario describes the behaviour in business words, and the steps are carried out by code behind them.
Should Gherkin steps use "I" or the third person?
Either works. Keep one voice per file, and use names when two people take part.
How long should a Gherkin scenario be?
The Gherkin reference recommends 3 to 5 steps.
Can a scenario have more than one When?
The parser allows it. It usually means two behaviours, which are clearer as two scenarios.
What is the difference between declarative and imperative Gherkin?
Imperative steps say how: type here, click there. Declarative steps say what: sign in, place an order. Cucumber's documentation recommends declarative.
Where Gherkin Studio fits
Gherkin Studio is a BDD app for Jira Cloud, built on Atlassian Forge. Its editor checks the syntax as you type, inside the Jira story, and suggests steps your team has already written. It is on the Atlassian Marketplace, with a 30-day free trial. See also how to write scenarios in a Jira story.