Guide
What is Gherkin? Syntax, keywords and examples
By Miftode Vasilica, maker of Gherkin Studio · · updated
Gherkin is the plain-text language of .feature files. A file holds one Feature. Under it come scenarios, and each scenario is a list of steps that start with Given, When, Then, And or But. Most lines begin with a keyword, indentation is free, and a line that starts with # is a comment. This page goes through every keyword with an example. Each example was run through the official Cucumber parser, @cucumber/gherkin version 42.
What Gherkin is used for
Gherkin is the language of behaviour-driven development (BDD). One scenario does three jobs at once:
- A shared specification. The product owner, the developers and the testers agree on examples of what a story should do, and write them in words all of them can read.
- An automated test. Tools such as Cucumber, behave, Reqnroll and pytest-bdd match each step to a piece of code and run the scenario as a test.
- Documentation that stays true. The scenarios run with every build, so one that no longer describes the software fails instead of going quietly out of date.
Gherkin is not a programming language. It has no loops, no conditions and no functions. The code lives in step definitions, written in the language of your project. See one story followed from Jira to a passing test in BDD examples, and habits that keep scenarios readable in how to write good Gherkin scenarios.
A whole file, read line by line
Here is one file that uses almost every keyword. The sections below explain each part.
# language: en
@PROJ-12
Feature: Password reset
Customers who forget their password get a link by email.
Background:
Given a customer "[email protected]" with a password
Rule: A reset link works for one hour
Example: Opened after 59 minutes
Given I requested a reset link 59 minutes ago
When I open the link
Then I can choose a new password
Scenario Outline: Opened too late
Given I requested a reset link <minutes> minutes ago
When I open the link
Then I see "This link has expired"
Examples:
| minutes |
| 61 |
| 1440 |
Rule: The reset email is short
@email
Example: The email a customer receives
When I ask for a reset link
Then I receive this email:
"""
Someone asked to reset your password.
The link works for one hour.
"""
| Line | What it is |
|---|---|
# language: en |
Optional. Sets the spoken language of the keywords. English is the default. |
@PROJ-12 |
A tag. It applies to everything in the feature. |
Feature: and the line under it |
The feature's name and a free-text description. |
Background: |
Steps that run before every scenario in the file. |
Rule: |
One business rule, with the examples that illustrate it. |
Example: |
One scenario. Same as Scenario:. |
Scenario Outline: and Examples: |
A scenario that runs once for each row of the table. |
<minutes> |
A placeholder, filled in from the column with that name. |
""" |
A doc string: a block of text passed to the step above it. |
The parser turns this file into four scenarios to run: the first example, the outline once for each of its two rows, and the email example. The background runs before each of the four.
Keywords at a glance
| Keyword | Also written | Where it goes | How many |
|---|---|---|---|
Feature: |
Business Need:, Ability: |
First keyword in the file | Exactly one per file |
Rule: |
Under the feature | Any number; rules do not nest | |
Background: |
Before the first scenario of a feature or of a rule | One per feature, one per rule | |
Scenario: |
Example: |
Under the feature or a rule | Any number |
Scenario Outline: |
Scenario Template: |
Under the feature or a rule | Any number |
Examples: |
Scenarios: |
Under a scenario outline | One or more per outline |
Given, When, Then, And, But |
* |
Under a scenario or a background | Any number |
""" or three backticks |
Right under a step | A doc string | |
| |
Under a step, or under Examples: |
A table | |
@ |
Above a feature, rule, scenario, outline or examples table | Any number | |
# |
At the start of any line | Any number |
Keywords with a colon keep the colon: Feature:, Rule:, Background:, Scenario:, Examples:. Step keywords take a space, never a colon.
Feature and its description
The first keyword in a file must be Feature:, after any tags, comments or language line. Only one feature fits in a file. A second Feature: is an error.
The lines under it, up to the first Background, Rule or scenario, are a free-text description. Cucumber ignores them when it runs, but reports show them. Write who the feature is for, or the rules in a sentence each. The one limit: no line of the description may start with a keyword.
Scenarios, outlines, backgrounds and rules can have a description too, in the same place: right under their own line.
Steps: Given, When, Then, And, But
Each step is one line: a keyword, a space, then any text.
Givenputs the system in a known state before anything happens.Whenis the action or event.Thenis the result you expect, something a person or another system can see.AndandButcontinue the step above them.*can replace any of them, when the steps read like a list:
Scenario: Everything on the list is in the basket
Given I am out shopping
* I have eggs
* I have milk
When I check my list
Then I do not need anything
The keyword is for people. When Cucumber looks for the code behind a step, it uses only the text after the keyword. So Given there is money in my account and Then there is money in my account are the same step to Cucumber.
Two mistakes that make a step disappear without an error. Both were checked with the parser.
Given:with a colon is not a step. The parser reads the line as description text. The step is simply not there.givenin lower case is not a step either. Keywords are case-sensitive, so the line becomes description text.
The file parses, the build is green, and the scenario checks less than you think. Three more mistakes of this kind, all valid Gherkin, are in how to write Gherkin scenarios in a Jira story.
Background
A Background: holds Given steps that every scenario in the feature shares. They run before each scenario. It goes before the first scenario, and there can be only one per feature. A rule can have its own background too.
Keep it short; the Cucumber reference suggests four lines at most. When a Background helps, and when it hides the scenario.
Scenario Outline and Examples
A Scenario Outline: is a scenario with placeholders in angle brackets. The Examples: table under it gives the values, one row per run. The first row holds the column names.
Scenario Outline: Eating cucumbers
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I have <left> cucumbers left
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
This runs twice. You can put a tag above an Examples: table, and the scenarios from its rows get that tag.
The current parser treats Scenario: and Scenario Outline: alike, so Examples: works under either. Scenario Outline and Examples: what runs, and what quietly does not.
Rule
Rule: has been part of Gherkin since version 6. It names one business rule and groups the examples that illustrate it. Rules do not nest, and a rule can have its own Background:. Scenarios that belong to no rule must come before the first Rule:.
Rules are the natural home for acceptance criteria: the rule is the criterion, the examples are the cases. Acceptance criteria in Given-When-Then format.
Data tables and doc strings
A step can carry more data than fits on one line.
A data table is a set of rows under the step, each cell between pipes. Every row needs the same number of cells. Inside a cell, write \| for a pipe, \n for a new line and \\ for a backslash.
Given these users exist:
| name | email |
| Ana | [email protected] |
| Ben | [email protected] |
A doc string is a block of text between two lines of """, or two lines of three backticks. You can name the content type after the opening line, for example """json. Inside, indentation counts from the column of the opening quotes.
The code behind the step receives the table or the text as its last argument. Data tables and doc strings: the rules nobody writes down.
Tags
A tag is @ followed by a word with no spaces. You can put several on one line. Tags go above a Feature:, Rule:, Scenario:, Scenario Outline: or Examples: line, and everything under that line inherits them.
Tags are how you choose what to run (--tags "@smoke") and how a scenario points to its Jira issue (@PROJ-12).
Two places do not take tags. A tag above Background: is a parser error, and so is a tag above a step:
(4:5): expected: #TagLine, #RuleLine, #Comment, #Empty, got 'Given x'
Link Cucumber scenarios to Jira issues with tags.
Comments
A comment is a whole line that starts with #, with or without spaces before it. Gherkin has no block comments.
A # in the middle of a line does not start a comment. In Given I sign in # remember me, the step text is I sign in # remember me, and Cucumber will look for code that matches all of it.
Spoken languages
The keywords exist in 80 languages in the parser we used. Put a language line first in the file:
# language: fr
Fonctionnalité: Mot de passe oublié
Scénario: Le lien a expiré
Soit un lien envoyé il y a 61 minutes
Without that first line, the parser expects English and stops at Fonctionnalité: with an error. Write in the language your users and domain experts speak; the Cucumber reference gives the same advice.
Indentation and blank lines
Indentation means nothing to the parser. You can use spaces or tabs, or no indentation at all, and the file reads the same. Two spaces per level is the usual choice, because people read the indentation even if the parser does not.
The one exception is inside a doc string, where the text keeps its indentation relative to the opening quotes. Blank lines are allowed anywhere.
The rules the parser enforces
These break the file with an error, so at least you hear about them:
- The first keyword is
Feature:. - One
Feature:per file. Background:comes before the first scenario, and only once per feature or rule.- Every row of a table has the same number of cells.
- Tags only above a feature, rule, scenario, outline or examples table.
- No free text after a step. Make it a comment, or move it above the steps.
Each error message, what it means and the fix: Gherkin syntax errors, explained in plain English.
Short answers
What is Gherkin?
A plain-text language for describing how software should behave, as scenarios with Given, When and Then steps. Tools such as Cucumber run those scenarios as tests.
What is Gherkin used for?
For writing examples a whole team can agree on, which then run as automated tests and stay true as documentation.
What file extension does Gherkin use?
.feature. One feature per file.
Is Gherkin case-sensitive?
The keywords are. given in lower case is read as description text, not as a step.
Does indentation matter in Gherkin?
Not to the parser. It matters inside doc strings, and to the people reading.
What is the difference between Scenario and Example?
There is no difference. They are two spellings of the same keyword.
What is the difference between Scenario and Scenario Outline?
An outline has placeholders and an Examples: table, and runs once per row. A plain scenario runs once.
Can I write Gherkin in my own language?
Yes. Put # language: xx on the first line, with the language code, and use that language's keywords.
Where Gherkin Studio fits
Gherkin Studio is a BDD app for Jira Cloud, built on Atlassian Forge. Its editor colours the keywords and checks the syntax as you type, inside the Jira story. It is on the Atlassian Marketplace, with a 30-day free trial. See also what a Cucumber feature file looks like, and how it runs.