QA How-To
Writing good Gherkin scenarios (2026)
Master writing good Gherkin scenarios with concrete examples, declarative steps, Scenario Outlines, tables, tags, review rules, and automation guidance.
22 min read | 3,167 words
TL;DR
Use domain language, one behavior per scenario, Given for relevant state, When for the event, and Then for observable outcomes. Prefer concrete examples, small tables, and Scenario Outlines only when rows test the same rule.
Key Takeaways
- Start each scenario from a business rule and concrete example.
- Use Given for state, When for the event, and Then for observable outcomes.
- Describe domain behavior instead of browser mechanics.
- Use Scenario Outlines only when every row tests the same rule.
- Keep backgrounds, tables, and tags small and purposeful.
- Review feature files with product, development, and QA roles.
writing good Gherkin scenarios is best approached as an engineering problem with explicit boundaries, fast feedback, and evidence that the result works. Writing good Gherkin scenarios means describing one valuable behavior with concrete examples, clear preconditions, a single action, and observable outcomes. The feature file should communicate business rules, not reproduce browser clicks.
This guide turns that goal into a repeatable workflow. It favors maintainable design, observable failures, and examples a working QA or SDET team can adapt without depending on hidden conventions.
TL;DR
Use domain language, one behavior per scenario, Given for relevant state, When for the event, and Then for observable outcomes. Prefer concrete examples, small tables, and Scenario Outlines only when rows test the same rule.
| Decision | Recommended default | Why |
|---|---|---|
| Declarative step | When the customer pays by card | Describes business action |
| Imperative step | When I click the Pay button | Leaks UI mechanics |
| Focused outcome | Then the order is confirmed | States observable value |
| Vague outcome | Then it works | Cannot guide implementation |
1. Start with a Business Rule, Not a Test Script
A feature begins with a capability and a rule stakeholders care about. Discuss examples before writing syntax: who acts, what state matters, what event occurs, and what observable result proves the rule. This example-mapping conversation is the primary value of BDD. Automation follows the shared understanding.
Do not convert an existing manual click script line by line. That produces brittle Gherkin and gives product readers no useful abstraction. A scenario for a declined payment should explain the decline behavior, not the sequence of fields and buttons used by the current screen.
A useful review asks three questions: what contract is expressed, where failure evidence appears, and which layer owns the correction. Apply that review to a representative happy path, a validation failure, and an infrastructure failure. The resulting differences reveal accidental coupling that a simple green run will not show.
Put this part into practice with a small, reviewable exercise focused on start with a business rule, not a test script. Choose one production-like example, write down its preconditions and expected evidence, and execute it twice from a clean state. On the second pass, introduce one controlled failure and confirm that the message identifies the responsible capability. Record any hidden dependency you discover, including shared data, execution order, environment assumptions, or undocumented configuration. Then make the narrowest improvement and repeat the exercise in CI. This method turns writing good Gherkin scenarios from a set of preferences into an observable engineering standard. It also gives reviewers concrete evidence instead of style arguments, which is especially important as contributor count grows.
2. Use Given, When, and Then with Discipline
Given establishes only state relevant to the behavior. When describes the event or action under evaluation. Then describes an observable outcome. And can extend the same phase, but a long chain usually signals too much scope.
One primary When creates a clear causal story. Multiple user actions may be legitimate for a workflow, yet consider whether earlier actions belong in Given as established state or whether the scenario should be split. Grammar is less important than an unambiguous contract.
A useful review asks three questions: what contract is expressed, where failure evidence appears, and which layer owns the correction. Apply that review to a representative happy path, a validation failure, and an infrastructure failure. The resulting differences reveal accidental coupling that a simple green run will not show.
Put this part into practice with a small, reviewable exercise focused on use given, when, and then with discipline. Choose one production-like example, write down its preconditions and expected evidence, and execute it twice from a clean state. On the second pass, introduce one controlled failure and confirm that the message identifies the responsible capability. Record any hidden dependency you discover, including shared data, execution order, environment assumptions, or undocumented configuration. Then make the narrowest improvement and repeat the exercise in CI. This method turns writing good Gherkin scenarios from a set of preferences into an observable engineering standard. It also gives reviewers concrete evidence instead of style arguments, which is especially important as contributor count grows.
3. Write Declarative Domain Language
Use words customers, analysts, support staff, and developers share. A declarative step says the shopper has an item in the cart; an imperative step says the shopper clicks a selector, waits two seconds, and reads a label. The first can survive a UI redesign and can be automated through UI or API.
Declarative does not mean vague. Include values that distinguish the example, such as currency, account state, or policy threshold. Hide mechanics, not business facts. See the BDD automation design guide for keeping bindings thin.
A useful review asks three questions: what contract is expressed, where failure evidence appears, and which layer owns the correction. Apply that review to a representative happy path, a validation failure, and an infrastructure failure. The resulting differences reveal accidental coupling that a simple green run will not show.
Put this part into practice with a small, reviewable exercise focused on write declarative domain language. Choose one production-like example, write down its preconditions and expected evidence, and execute it twice from a clean state. On the second pass, introduce one controlled failure and confirm that the message identifies the responsible capability. Record any hidden dependency you discover, including shared data, execution order, environment assumptions, or undocumented configuration. Then make the narrowest improvement and repeat the exercise in CI. This method turns writing good Gherkin scenarios from a set of preferences into an observable engineering standard. It also gives reviewers concrete evidence instead of style arguments, which is especially important as contributor count grows.
4. Choose Concrete Examples That Expose Rules
Examples should sit near boundaries: just below, at, and above a threshold; valid and invalid state transitions; eligible and ineligible roles. A scenario titled Discount is too broad, while Member receives free shipping for an order over $50 communicates the rule and example.
Avoid random values in feature text. Deterministic values make discussion and failures reproducible. Randomized exploration belongs in property-based or specialized tests, not in a specification example whose job is shared understanding.
A useful review asks three questions: what contract is expressed, where failure evidence appears, and which layer owns the correction. Apply that review to a representative happy path, a validation failure, and an infrastructure failure. The resulting differences reveal accidental coupling that a simple green run will not show.
Put this part into practice with a small, reviewable exercise focused on choose concrete examples that expose rules. Choose one production-like example, write down its preconditions and expected evidence, and execute it twice from a clean state. On the second pass, introduce one controlled failure and confirm that the message identifies the responsible capability. Record any hidden dependency you discover, including shared data, execution order, environment assumptions, or undocumented configuration. Then make the narrowest improvement and repeat the exercise in CI. This method turns writing good Gherkin scenarios from a set of preferences into an observable engineering standard. It also gives reviewers concrete evidence instead of style arguments, which is especially important as contributor count grows.
5. Use Scenario Outlines Without Creating Spreadsheets
A Scenario Outline is appropriate when each row exercises the same rule and the same steps. Keep columns limited to facts that change the expected behavior. If rows require conditional steps or have different reasons to fail, split them into named scenarios.
Make example headings domain-specific and add a description when categories matter. Several small Examples blocks can communicate valid and invalid partitions more clearly than one large table.
A useful review asks three questions: what contract is expressed, where failure evidence appears, and which layer owns the correction. Apply that review to a representative happy path, a validation failure, and an infrastructure failure. The resulting differences reveal accidental coupling that a simple green run will not show.
Put this part into practice with a small, reviewable exercise focused on use scenario outlines without creating spreadsheets. Choose one production-like example, write down its preconditions and expected evidence, and execute it twice from a clean state. On the second pass, introduce one controlled failure and confirm that the message identifies the responsible capability. Record any hidden dependency you discover, including shared data, execution order, environment assumptions, or undocumented configuration. Then make the narrowest improvement and repeat the exercise in CI. This method turns writing good Gherkin scenarios from a set of preferences into an observable engineering standard. It also gives reviewers concrete evidence instead of style arguments, which is especially important as contributor count grows.
Feature: Shipping eligibility
Rule: Members receive free shipping at $50
Scenario Outline: Shipping fee follows the member threshold
Given a member has an order worth <order_total> dollars
When shipping is calculated
Then the shipping fee is <fee> dollars
Examples: Around the threshold
| order_total | fee |
| 49.99 | 5 |
| 50.00 | 0 |
| 50.01 | 0 |
6. Use Data Tables and Doc Strings Intentionally
A data table is valuable when the shape itself aids understanding, such as several line items or a field-value map. It is not a substitute for a hidden database fixture. Keep tables small enough to review without horizontal scrolling.
Doc Strings suit meaningful multiline content such as a message template or JSON request contract. Do not embed enormous payloads. Store bulky technical fixtures near adapter tests and keep the feature focused on the business differences.
A useful review asks three questions: what contract is expressed, where failure evidence appears, and which layer owns the correction. Apply that review to a representative happy path, a validation failure, and an infrastructure failure. The resulting differences reveal accidental coupling that a simple green run will not show.
Put this part into practice with a small, reviewable exercise focused on use data tables and doc strings intentionally. Choose one production-like example, write down its preconditions and expected evidence, and execute it twice from a clean state. On the second pass, introduce one controlled failure and confirm that the message identifies the responsible capability. Record any hidden dependency you discover, including shared data, execution order, environment assumptions, or undocumented configuration. Then make the narrowest improvement and repeat the exercise in CI. This method turns writing good Gherkin scenarios from a set of preferences into an observable engineering standard. It also gives reviewers concrete evidence instead of style arguments, which is especially important as contributor count grows.
7. Name Scenarios for Behavior and Outcome
A strong name summarizes context and expected result, such as Locked customer cannot reset a password. Avoid Scenario 1, Verify login, or titles that repeat the feature name without distinguishing the rule. Test reports depend on these names when diagnosing failures.
Use consistent active language. The title should remain accurate even if the implementation changes from web to mobile. Tags add operational metadata, but they cannot rescue an unclear name.
A useful review asks three questions: what contract is expressed, where failure evidence appears, and which layer owns the correction. Apply that review to a representative happy path, a validation failure, and an infrastructure failure. The resulting differences reveal accidental coupling that a simple green run will not show.
Put this part into practice with a small, reviewable exercise focused on name scenarios for behavior and outcome. Choose one production-like example, write down its preconditions and expected evidence, and execute it twice from a clean state. On the second pass, introduce one controlled failure and confirm that the message identifies the responsible capability. Record any hidden dependency you discover, including shared data, execution order, environment assumptions, or undocumented configuration. Then make the narrowest improvement and repeat the exercise in CI. This method turns writing good Gherkin scenarios from a set of preferences into an observable engineering standard. It also gives reviewers concrete evidence instead of style arguments, which is especially important as contributor count grows.
8. Apply Background, Rule, and Tags Sparingly
Background is for short shared context essential to every scenario in its scope. If readers must scroll up to reconstruct a large background, the scenarios are no longer independently understandable. Prefer a domain-level state over a dozen setup steps.
Rule groups scenarios under a business rule and can make feature intent much clearer. Tags should support purposeful selection or metadata, such as @smoke or @payments, with documented ownership. Avoid tagging every organizational dimension. Learn selection discipline in the test suite tagging guide.
A useful review asks three questions: what contract is expressed, where failure evidence appears, and which layer owns the correction. Apply that review to a representative happy path, a validation failure, and an infrastructure failure. The resulting differences reveal accidental coupling that a simple green run will not show.
Put this part into practice with a small, reviewable exercise focused on apply background, rule, and tags sparingly. Choose one production-like example, write down its preconditions and expected evidence, and execute it twice from a clean state. On the second pass, introduce one controlled failure and confirm that the message identifies the responsible capability. Record any hidden dependency you discover, including shared data, execution order, environment assumptions, or undocumented configuration. Then make the narrowest improvement and repeat the exercise in CI. This method turns writing good Gherkin scenarios from a set of preferences into an observable engineering standard. It also gives reviewers concrete evidence instead of style arguments, which is especially important as contributor count grows.
9. Connect Gherkin to Maintainable Step Definitions
Bind one domain phrase to one cohesive automation action. Step definitions should translate language, delegate to domain workflows or adapters, and return useful failures. Avoid putting selectors, raw SQL, and complex branching directly inside bindings.
Parameter types should be explicit and validated. Reusing a generic step like I click X across every feature appears efficient but recreates an imperative test language. Prefer bounded vocabulary that expresses the product domain. The automation framework structure guide shows a suitable lower layer.
A useful review asks three questions: what contract is expressed, where failure evidence appears, and which layer owns the correction. Apply that review to a representative happy path, a validation failure, and an infrastructure failure. The resulting differences reveal accidental coupling that a simple green run will not show.
Put this part into practice with a small, reviewable exercise focused on connect gherkin to maintainable step definitions. Choose one production-like example, write down its preconditions and expected evidence, and execute it twice from a clean state. On the second pass, introduce one controlled failure and confirm that the message identifies the responsible capability. Record any hidden dependency you discover, including shared data, execution order, environment assumptions, or undocumented configuration. Then make the narrowest improvement and repeat the exercise in CI. This method turns writing good Gherkin scenarios from a set of preferences into an observable engineering standard. It also gives reviewers concrete evidence instead of style arguments, which is especially important as contributor count grows.
10. Review Scenarios as Product Artifacts
Review feature files with product, development, and QA perspectives. Ask whether the rule is correct, whether examples cover meaningful partitions, whether outcomes are observable, and whether language matches the domain. Then have automation reviewers inspect binding ambiguity and implementation cost.
A scenario is done when stakeholders agree on meaning and the automated check produces trustworthy evidence. Passing syntax alone is not quality. Periodically remove obsolete scenarios and merge duplicates so the suite remains an active specification.
A useful review asks three questions: what contract is expressed, where failure evidence appears, and which layer owns the correction. Apply that review to a representative happy path, a validation failure, and an infrastructure failure. The resulting differences reveal accidental coupling that a simple green run will not show.
Put this part into practice with a small, reviewable exercise focused on review scenarios as product artifacts. Choose one production-like example, write down its preconditions and expected evidence, and execute it twice from a clean state. On the second pass, introduce one controlled failure and confirm that the message identifies the responsible capability. Record any hidden dependency you discover, including shared data, execution order, environment assumptions, or undocumented configuration. Then make the narrowest improvement and repeat the exercise in CI. This method turns writing good Gherkin scenarios from a set of preferences into an observable engineering standard. It also gives reviewers concrete evidence instead of style arguments, which is especially important as contributor count grows.
Interview Questions and Answers
Q: What is Gherkin?
Gherkin is a structured language for expressing behavior with features, rules, scenarios, and Given, When, Then steps. Its purpose is shared executable specification, not merely test automation syntax.
Q: What is the role of Given?
Given establishes relevant preconditions and context. It should not contain the behavior being tested or unnecessary setup detail.
Q: What is the role of When?
When describes the event or action whose effect the scenario examines. A focused scenario usually has one primary When.
Q: What is the role of Then?
Then states observable outcomes that prove the rule. It should avoid implementation details and vague success statements.
Q: Scenario versus Scenario Outline?
A Scenario is one concrete example. A Scenario Outline runs the same behavioral structure for multiple rows of examples.
Q: When is Background harmful?
It is harmful when long, mutable, or irrelevant to some scenarios. Readers should not need to decode extensive hidden setup.
Q: How do you prevent step duplication?
I align vocabulary around domain concepts, search existing steps, and scope language by domain when needed. I do not force unrelated meanings into a generic step.
Q: What is declarative Gherkin?
It describes what the actor accomplishes and what the system guarantees. It avoids encoding clicks, selectors, waits, and other implementation mechanics.
Q: How do you select examples?
I use equivalence classes, boundaries, state transitions, risks, and known business disputes. Each example should reveal something about the rule.
Q: Should assertions be in step definitions?
Then bindings commonly delegate to assertion-capable domain helpers, but complex test logic should not accumulate in bindings. Failures must preserve the business expectation.
Q: How do tags help?
Tags support intentional selection and metadata such as capability or execution lane. A controlled taxonomy prevents contradictory or unused tags.
Q: How do you review Gherkin quality?
I check rule clarity, concrete examples, causal flow, observable outcomes, domain language, independence, and automation feasibility. Stakeholder agreement is as important as syntax.
Common Mistakes
- Writing click-by-click UI instructions in business scenarios.
- Combining several independent behaviors under multiple When steps.
- Using vague outcomes such as successful or works correctly.
- Building huge Scenario Outline tables with unrelated partitions.
- Hiding important business facts inside fixtures or step code.
- Creating generic reusable steps that form a second programming language.
Treat mistakes as signals about system design, not as reasons to add retries blindly. Record the failure mode, improve the narrowest responsible layer, and keep the correction visible in review.
Conclusion
Writing good Gherkin scenarios is an act of product design: identify the rule, choose revealing examples, express one causal story, and automate below the domain language. Start with one representative workflow, prove it locally and in CI, then expand using the same conventions. That sequence gives the team a trustworthy baseline and makes later improvements measurable.
Interview Questions and Answers
What is Gherkin?
Gherkin is a structured language for expressing behavior with features, rules, scenarios, and Given, When, Then steps. Its purpose is shared executable specification, not merely test automation syntax.
What is the role of Given?
Given establishes relevant preconditions and context. It should not contain the behavior being tested or unnecessary setup detail.
What is the role of When?
When describes the event or action whose effect the scenario examines. A focused scenario usually has one primary When.
What is the role of Then?
Then states observable outcomes that prove the rule. It should avoid implementation details and vague success statements.
Scenario versus Scenario Outline?
A Scenario is one concrete example. A Scenario Outline runs the same behavioral structure for multiple rows of examples.
When is Background harmful?
It is harmful when long, mutable, or irrelevant to some scenarios. Readers should not need to decode extensive hidden setup.
How do you prevent step duplication?
I align vocabulary around domain concepts, search existing steps, and scope language by domain when needed. I do not force unrelated meanings into a generic step.
What is declarative Gherkin?
It describes what the actor accomplishes and what the system guarantees. It avoids encoding clicks, selectors, waits, and other implementation mechanics.
How do you select examples?
I use equivalence classes, boundaries, state transitions, risks, and known business disputes. Each example should reveal something about the rule.
Should assertions be in step definitions?
Then bindings commonly delegate to assertion-capable domain helpers, but complex test logic should not accumulate in bindings. Failures must preserve the business expectation.
How do tags help?
Tags support intentional selection and metadata such as capability or execution lane. A controlled taxonomy prevents contradictory or unused tags.
How do you review Gherkin quality?
I check rule clarity, concrete examples, causal flow, observable outcomes, domain language, independence, and automation feasibility. Stakeholder agreement is as important as syntax.
Frequently Asked Questions
What makes a good Gherkin scenario?
It explains one business behavior through relevant state, one clear event, and observable outcomes. It is concrete enough to test and abstract enough to survive implementation changes.
Can a scenario have multiple When steps?
It can, but multiple actions often indicate more than one behavior or misplaced setup. Prefer one causal event when possible.
When should I use a Scenario Outline?
Use it when several examples share identical steps and validate the same rule. Split examples when their meaning or workflow differs.
Should Gherkin mention UI elements?
Only when the UI itself is the business contract, such as an accessibility or disclosure requirement. Most scenarios should use domain actions.
How long should a scenario be?
There is no universal line limit. It should be independently understandable and focused, usually with a small number of steps.
Who should review feature files?
Product, development, and QA should review meaning and examples together. Automation specialists should also review binding and execution design.