Gherkin feature files are presented as readable, structured documentation for a single area of behavior, with the overall goal of keeping scenarios clear enough for both people and automation tools. The chapter introduces the core building blocks of a feature file: a single feature declaration, optional descriptive text, business rules that organize related scenarios, and scenarios that capture concrete examples of behavior discovered with the team. It also emphasizes that the wording and structure should reflect business language, remain consistent, and be treated as living documentation once automated.
The chapter explains how to formulate scenarios by separating context, action, and outcome using the step keywords Given, When, and Then. Context should describe only what is essential, usually in the past tense, while When should capture the single triggering action and Then should state the expected result. It warns against overloading scenarios with unnecessary detail, using multiple actions where one is enough, or combining ideas that really belong in separate examples. Conjunctions such as And and But can improve readability, but they should be used carefully because they often signal that a step may need to be split or rethought.
It also covers Gherkin features that help keep text readable when scenarios become more complex. Data tables are recommended for structured information such as orders, prices, or other tabular data, while scenario outlines and examples tables are used to express several similar scenarios or data-driven variations without repeating the same structure. The chapter closes by showing how docstrings can represent long blocks of text such as multi-line messages, preserving exact formatting when needed. Throughout, the main message is to prefer concise, readable formulations that highlight the important business rule rather than implementation detail.
Summary
Gherkin has two types of keywords: block keywords and step keywords.
The step keywords are Given, When, Then, and two conjunctions, And and But.
Given sets up the context by describing the state the system is in before the behavior being illustrated is exhibited.
When describes the stimulus or event that causes the system to behave in a particular way. There should be only a single When step in a scenario, except in the rare case that two relevant triggering actions take place simultaneously.
Then describes expected outcome of the action. There should be only a single Then step in a scenario, unless the rule being illustrated results in multiple related outcomes.
The conjunctions And and But indicate a continuation when used after Given, When, or Then.
Gherkin keywords are available in multiple languages and its focus is to enable the writing of idiomatic natural language specifications.
The steps in an illustrative scenario should include only information that is essential to illustrating a specific rule.
Data tables make structured data needed by a step easier to represent and read.
Scenario outlines can be used to make small variations between several scenarios easier to read and reason about.
The docstring construct is provided when you need to document long blocks of text within which whitespace is important.
FAQ
What is the main purpose of a feature file in Gherkin?A feature file organizes scenarios into a readable form of documentation that describes a single area of product functionality.How should a feature file be structured at the top level?A feature file must start with one Feature: keyword, and it may include a feature description followed by rules and scenarios.What is the role of the Rule keyword in a feature file?The Rule keyword documents a business rule discussed during discovery and groups the scenarios that illustrate that rule.What do the Given, When, and Then keywords represent?Given describes the context, When describes the action that triggers the behavior, and Then describes the expected outcome.When should you use And or But in a scenario?Use And or But to continue a step in a readable way, usually to extend context, and in some cases to extend actions or outcomes when that is clearer.How do you decide whether a statement belongs in Given or When?Ask whether the statement is pre-existing context or the trigger for the behavior. If it is part of the state before the behavior, it is usually Given; if it causes the behavior, it is usually When.Why should scenarios usually have only one When step?Because a scenario should focus on a single triggering action. Multiple When steps often indicate that one of the statements is actually context or that the scenario needs to be reformulated.When is it acceptable to have more than one Then step?It is acceptable when the outcomes are tightly coupled and all belong to the same rule, such as preventing progress and explaining why progress was blocked.What is the difference between a data table and a scenario outline examples table?A data table provides structured data to the step immediately above it, while an examples table supplies parameter values that are substituted into a scenario outline to generate multiple scenarios.When should you use a docstring in Gherkin?Use a docstring when you need to provide a large block of text or preserve formatting and whitespace, such as for multi-line error messages.
pro $24.99 per month
access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!