Gherkin is the plain-text language used to write Behaviour-Driven Development (BDD) scenarios. A Gherkin file describes a feature as a set of concrete examples, each made of Given, When and Then steps. Test frameworks such as Cucumber match each step to code, so the same file is both a specification and an automated test. New to BDD? Start with what Behaviour-Driven Development is.
Gherkin keywords
| Keyword | What it does |
|---|---|
Feature | Names the feature and holds its scenarios. One per .feature file. |
Rule | Groups scenarios that illustrate one business rule (Gherkin 6+). |
Background | Steps that run before every scenario in the feature or rule. |
Scenario (or Example) | One concrete example of one behaviour. |
Given | The starting situation (context). |
When | The action or event. |
Then | The expected outcome. |
And, But | Continue the previous Given, When or Then. |
* | A bullet that can replace any step keyword. |
Scenario Outline (or Scenario Template) | A scenario run once for each row of its Examples table. |
Examples (or Scenarios) | The table of values for a Scenario Outline. |
@tag | Labels a feature, rule, scenario or Examples table. |
""" | Doc string: a multi-line text argument for a step. |
| | Data table cell separator. |
# | Comment line. |
Gherkin file structure
A .feature file starts with Feature: and a name. Free text below it describes the feature; Gherkin ignores it until the next keyword. Scenarios follow, each with a name and its steps. Indentation is not significant, but two spaces per level is the convention.
Feature: Discount codes
Customers can apply one discount code per order.
Scenario: A valid code reduces the total
Given my basket contains items worth £50.00
When I apply the discount code "WELCOME10"
Then the order total is £45.00Given, When, Then (and And, But)
Given puts the system in a known state, When describes the one action under test, and Then states the observable outcome. And and But continue the previous step type and keep scenarios readable. Cucumber treats them all the same when matching steps; the keywords are for the reader.
Given I have 2 items in my basket
And one of them is on sale
When I check out
Then I pay the sale price for that item
But I pay the full price for the otherBackground
A Background holds Given steps shared by every scenario in a feature (or in a Rule). Keep it short; if readers have to scroll up to understand a scenario, move the steps back into it.
Scenario Outline and Examples
When one rule needs checking with several values, write a Scenario Outline with <placeholders> and an Examples table. The scenario runs once per row.
Scenario Outline: Password strength
When I choose the password "<password>"
Then I see the strength "<strength>"
Examples:
| password | strength |
| abc | weak |
| abc12345 | medium |
| R7!vq-29xLm# | strong |Data tables
A data table passes a list or a table to a single step. Rows are separated by new lines and cells by |. Unlike Examples, a data table does not repeat the scenario.
Given the following users exist:
| name | role |
| Maria | admin |
| Tom | viewer |Doc strings
A doc string passes a block of text, such as JSON or an email body, to a step. Wrap it in three double quotes (""") or three backticks.
Tags
Tags start with @ and sit on the line above a Feature, Rule, Scenario or Examples. Tags on a feature apply to all its scenarios. Teams use them to select test runs (@smoke), link to Jira issues (@PROJ-123) or mark work in progress. See organising Cucumber scenarios with tags.
Gherkin in other languages
Gherkin keywords exist in more than 70 languages. Declare the language on the first line, for example # language: de for German (Funktionalität, Szenario, Angenommen, Wenn, Dann), # language: es for Spanish (Característica, Escenario, Dado, Cuando, Entonces), # language: fr for French or # language: pt for Portuguese (Funcionalidade, Cenário, Dado, Quando, Então). The online editor and AssertThat support these too.
Best practices for writing Gherkin
- One behaviour per scenario, with exactly one
When. - Write what the user achieves, not which buttons they click.
- Use real example values instead of vague words like "some" or "valid".
- Name scenarios after the outcome, so a list of names reads like a specification.
- Keep scenarios to about three to seven steps.
- Reuse step wording so steps map to the same step definitions.
Given-When-Then examples
Ten scenarios covering common situations. Copy one into the online Gherkin editor and adapt it.
1. Login with valid credentials
Scenario: Registered user signs in
Given a registered user "maria@example.com"
When she signs in with the correct password
Then she sees her dashboard2. Account locked after failed attempts
Scenario: Account is locked after three failed sign-ins
Given a registered user "maria@example.com"
And she has failed to sign in 2 times
When she signs in with a wrong password
Then her account is locked
And she receives an unlock email3. Shopping basket total
Scenario: Basket total includes every item
Given my basket contains:
| product | price |
| Notebook | 4.50 |
| Pen | 1.20 |
When I view my basket
Then the total is 5.704. Free delivery threshold (Scenario Outline)
Scenario Outline: Delivery charge depends on order value
Given my order is worth <value>
When I go to checkout
Then the delivery charge is <charge>
Examples:
| value | charge |
| 19.99 | 3.95 |
| 20.00 | 0.00 |
| 45.00 | 0.00 |5. Search with no results
Scenario: Search with no matches
Given the catalogue has no product called "unicorn lamp"
When I search for "unicorn lamp"
Then I see "No products found"
And I see suggestions for popular products6. API response (doc string)
Scenario: Create an order through the API
When I send a POST request to "/orders" with:
"""
{ "sku": "NB-01", "quantity": 2 }
"""
Then the response status is 201
And the response contains an order id7. Role-based access
Scenario: Viewers cannot delete projects
Given I am signed in as a "viewer"
When I open the project settings
Then I do not see the "Delete project" button8. Background for shared setup
Feature: Password reset
Background:
Given a registered user "maria@example.com"
Scenario: Reset link is sent
When she requests a password reset
Then she receives a reset email
Scenario: Reset link expires
Given she requested a reset 25 hours ago
When she opens the reset link
Then she sees "This link has expired"9. Business rule with Rule
Feature: Loyalty points
Rule: Customers earn 1 point per whole pound spent
Example: Whole pounds only
When a customer spends 12.80
Then they earn 12 points
Example: No points for refunds
Given a customer earned 12 points on an order
When the order is refunded
Then the 12 points are removed10. Tags for test selection
@checkout @smoke
Scenario: Guest checkout
Given I am not signed in
When I check out as a guest
Then I am asked for my email addressWriting scenarios in Jira? AssertThat BDD & Cucumber for Jira gives you this syntax with autocomplete and validation inside Jira issues, and runs them with Cucumber.