Given When Then: Writing Acceptance Criteria in Gherkin
Given When Then is a sentence pattern for writing acceptance criteria as concrete, testable examples. This guide covers the Gherkin keywords, a complete example, the declarative style and the mistakes that most often make scenarios hard to test.
Given When Then describes behavior as a starting state, an event and an outcome
Given When Then is a format for acceptance criteria in which Given sets up the starting state, When names the action or event and Then states the outcome someone can observe. Each criterion becomes a concrete example that product, development and QA read the same way and that a tool can run as an automated test. Gherkin is the structured language with fixed keywords that the testing tool Cucumber uses to read these examples.
The pattern comes from behavior-driven development (BDD), a way of working where the team first describes the desired behavior through examples and then builds it. Dan North described it in his 2006 article "Introducing BDD" and credits business analyst Chris Matts as his partner in shaping it. North's template reads: given some initial context, when an event occurs, then ensure some outcomes.
You reach for Gherkin as soon as a user story gets acceptance criteria. Acceptance criteria define when that one story is satisfied. Given When Then is one way to write them down. The Definition of Done applies to every story a team delivers and doesn't use this pattern. An epic is better served by success criteria. A bug report needs steps to reproduce.
Writing a scenario in four steps
A scenario shows exactly one behavior through one concrete case. Work through it in this order:
- Name the rule: write down in one sentence which business rule the scenario illustrates, such as "A delivery slot can't be booked once it is full." That sentence becomes the scenario name.
- Set the starting state: Given describes the situation before anything happens, with concrete values, such as "Given the 6 to 8 pm slot on Friday has 1 spot left." The Cucumber reference advises against describing user interaction in Given steps.
- Pick one event: When describes a single action or event. The reference suggests writing it with no assumptions about technology or the user interface. Its rule of thumb is to word the step the way you would have in 1922, before screens existed.
- Make the outcome observable: Then describes what a person or another system sees or receives afterward. According to the reference, database contents are usually not a good outcome, because nobody outside the system observes them.
And adds further starting conditions or outcomes. But adds an outcome that rules something out. The Cucumber reference recommends three to five steps per scenario. A longer scenario usually checks more than one rule and should be split.
Gherkin keywords in English and German
Gherkin is available in more than 70 languages. A file whose first line is "# language: de" uses German keywords. Without that header, Gherkin assumes English. The reference advises writing in the language your domain experts use. The German keywords below come from Cucumber's official language list:
| English | German | Purpose |
|---|---|---|
| Feature | Funktionalität | groups the scenarios for one feature |
| Rule | Regel | groups the scenarios for one business rule |
| Scenario or Example | Szenario or Beispiel | one concrete example of a rule |
| Background | Grundlage or Hintergrund | context that applies before every scenario |
| Given | Angenommen, Gegeben sei | starting state |
| When | Wenn | action or event |
| Then | Dann | observable outcome |
| And, But | Und, Aber | further steps of the same kind |
| Scenario Outline | Szenariogrundriss | template with placeholders |
| Examples | Beispiele | table of values for the outline |
Two details trip people up. Feature, Rule, Scenario, Background, Scenario Outline and Examples take a colon, while steps don't. The reference warns that a colon after the wrong keyword makes Cucumber ignore your tests. Also, keywords don't count when Cucumber matches a step to its code, so "Given the cart is empty" and "Then the cart is empty" are the same step to the tool. For German teams there is a third trap: the bare word "Gegeben" that many people write in tickets is not a valid keyword. A .feature file needs "Angenommen", "Gegeben sei" or "Gegeben seien".
On a ticket, many teams write a criterion as a single sentence: "Given ..., when ..., then ...". That works fine as long as nobody runs the criteria as automated tests.
Write declarative steps that describe behavior
A declarative scenario says what the system should do and leaves out how someone operates it. An imperative scenario spells out every click and every field. The Cucumber page "Writing better Gherkin" recommends the declarative style, because imperative scenarios are tied to the user interface and take more work to maintain. It also offers a check: will this wording need to change if the implementation does? If so, rewrite the step.
When Sam clicks "Delivery"
And Sam selects "Friday"
And Sam clicks the "6-8 pm" button
And Sam clicks "Confirm"
Then Sam sees the text "Booked"
When Sam books the Friday 6 to 8 pm delivery slot
Then Sam's order shows delivery on Friday between 6 and 8 pm
And Sam receives a confirmation email
The declarative version still holds when the slot picker turns into a calendar or the booking moves to a mobile app. It also names an outcome the business cares about: the customer gets a confirmation.
A good example from an online grocery store
An online grocery store lets customers choose a delivery slot at checkout. Each slot has a fixed number of spots. Customers should never end up booked into a slot the drivers can't serve. The example shows a feature with a background, two scenarios and a Scenario Outline:
Feature Choose a delivery slot
Customers pick a two-hour delivery slot so they know when to be home.
Background
Given Sam has a cart ready for checkout
Scenario Booking a slot with spots left
Given the Friday 6 to 8 pm slot has 1 spot left
When Sam books that slot
Then Sam's order shows delivery on Friday between 6 and 8 pm
And the slot shows as full to other customers
Scenario Trying to book a full slot
Given the Friday 6 to 8 pm slot is full
When Sam tries to book that slot
Then Sam sees that the slot is no longer available
And Sam sees the next open slots that day
But no booking is made for Sam
Scenario Outline Delivery fee depends on the order total
Given Sam's order total is <total>
When Sam books a delivery slot
Then the delivery fee is <fee>
Examples
| total | fee |
| $34.99 | $5.95 |
| $35.00 | $0.00 |
| $80.00 | $0.00 |
The background states once what every scenario builds on. Each scenario checks one rule in no more than five steps of its own. The second scenario uses But to state what must not happen. The Scenario Outline tests the free delivery threshold with three rows, including the boundary values just below and exactly at $35, without copying the scenario three times. A tester can verify every outcome without asking anyone.
A weak example and what it lacks
Scenario Delivery
Given the user is logged in
When he clicks on delivery and selects a slot and confirms
Then everything works
And a row is added to the delivery_slots table
The name "Delivery" doesn't say which rule is being checked. "The user" could be anyone. The starting state says nothing about how full the slot is, so no outcome can be predicted. The When step bundles three actions and describes clicks. Nobody can verify "everything works." The last step points to a database table, which no person observes. The scenario also leaves out the full slot and the delivery fee entirely.
Common mistakes
- One scenario checks several rules and grows long. Each scenario should illustrate one rule.
- Several When steps follow each other. That usually means two scenarios are hiding in one.
- Steps describe clicks, fields and buttons instead of behavior.
- Then names something invisible, such as a database record or an internal flag.
- The starting state is missing or so vague that the outcome is open.
- Values are fuzzy, such as "a few items" or "quickly." Concrete numbers make the scenario testable.
- The background keeps growing. The Cucumber reference suggests about four lines.
- Only the happy path is covered, with no boundaries or error cases.
Checklist
- The scenario name states the rule it illustrates.
- Given describes a state with concrete values and no user interaction.
- The scenario has exactly one When.
- Every Then describes an outcome a person or another system can observe.
- The scenario has three to five steps.
- The steps would stay valid if the user interface changed.
- At least one scenario covers a boundary or an error case.
- Repeated cases with different values sit in a Scenario Outline with Examples.
Check the story behind the criteria against INVEST. For how acceptance criteria fit into the wider process, see the guide to requirements engineering.
Write and check acceptance criteria with AI
The free tool on this site writes user stories with numbered acceptance criteria (AC 1, AC 2 and so on). You can turn each criterion into a Given When Then scenario as shown above. You describe what you need in your own words. When information is missing, the AI asks follow-up questions, for example about edge cases and error cases. It then writes the story with three to six criteria and checks it against INVEST. Anything the AI added on its own is listed separately so you can review it. Create a ticket with AI
Frequently asked questions
What does Given When Then mean?
Given When Then is a sentence pattern for acceptance criteria. Given describes the starting state and When the action or event. Then states the expected outcome. It turns a requirement into a concrete example you can test.
What is the difference between Gherkin and Cucumber?
Gherkin is the language you write scenarios in. Cucumber is a testing tool that reads Gherkin files and connects each step to code that runs it. You can use Gherkin without Cucumber, for example to write acceptance criteria on tickets.
How many steps should a Gherkin scenario have?
The Cucumber reference recommends three to five steps. Longer scenarios usually check several rules and stop working as clear examples. Split them into separate scenarios.
When should I use a Scenario Outline?
Use a Scenario Outline when the same rule needs checking with several sets of values. The steps contain placeholders in angle brackets. The Examples table below supplies one set of values per row. Cucumber runs the scenario once for each row.
Can I write Gherkin in languages other than English?
Yes. Gherkin supports more than 70 languages. Put a header such as "# language: de" or "# language: es" in the first line of the .feature file, then use that language's keywords. Without the header, Cucumber expects English.
Do all acceptance criteria have to use Given When Then?
No. The format suits behavior that plays out as a starting state, an event and an outcome. A rule like "The page is available in English and Spanish" reads better as a plain sentence.
Sources
- Cucumber: Gherkin Reference
- Cucumber: Writing better Gherkin
- Cucumber: Gherkin keyword list for all languages (gherkin-languages.json)
- Dan North: Introducing BDD (2006)
Last updated: