Specifying by Example
Workflow: Writing SBE specifications
Clarify the business goal. Ask what problem the feature solves and for whom. If the user provides a user story, extract the goal. If they provide raw requirements, ask "why are we building this?"
Identify rules. Extract the distinct business rules from the requirements. Each rule is a constraint or behavior the system must enforce. Name each rule explicitly.
Generate examples for each rule. For every rule, produce:
- The happy path (main success scenario).
- Key boundary conditions (edges where behavior changes).
- Important negative cases (what the system should refuse).
Use realistic data (real names, plausible amounts), not "foo"/"bar."
Write Gherkin scenarios. Convert each example into a Given-When-Then scenario following these rules:
- Declarative, not imperative. Describe what happens, not UI steps. No CSS selectors, button names, or page navigation.
- One behavior per scenario. Each scenario has exactly one When-Then pair.
- Essential data only. Include only data that illustrates the rule. Omit incidental details (user IDs, timestamps, irrelevant fields).
- Ubiquitous language. Use the same terms the business uses. No
user_id, POST /api/orders, or status = 2.
- Name the rule. Group scenarios under a
Rule: keyword in Gherkin.
Use Scenario Outlines for boundary tables. When a rule has many input-output combinations, use a parameterized Scenario Outline with an Examples table instead of repeating similar scenarios.
Check for specification gaps. Are there rules with no examples? Examples with no clear rule? Unanswered questions? Surface these explicitly.
Organize by domain concept. Group feature files by business capability (e.g., specs/pricing/, specs/accounts/), not by sprint or ticket number.
Workflow: Reviewing SBE specifications
- Read all scenarios in the feature file or specification document.
- Check each scenario against these anti-patterns:
| Anti-pattern |
Symptom |
Fix |
| Imperative scripts |
Steps say "click," "type," "navigate" |
Rewrite in declarative style |
| Multi-behavior scenarios |
Multiple When-Then pairs |
Split into one scenario per behavior |
| Incidental detail overload |
Irrelevant data (user IDs, timestamps) |
Strip to essential data only |
| Technical leakage |
Steps reference APIs, HTTP codes, DB tables |
Replace with business-language equivalents |
| Inconsistent vocabulary |
Same concept called different names |
Pick one term and use it everywhere |
| Scenario proliferation |
Hundreds of trivially different scenarios |
Use Scenario Outlines; push combinatorics to unit tests |
| Too abstract |
"Given sufficient funds / When they withdraw / Then it succeeds" |
Add concrete amounts and expected outcomes |
| UI-coupled specs |
Scenarios break when the UI is redesigned |
Automate against the API/service layer; use declarative language |
- Verify the specification covers happy path, boundaries, and negative cases for each rule.
- Propose specific fixes with before/after Gherkin.
Quick example
Input: "Users get free shipping on orders over $75, otherwise $5.95 flat rate."
Output:
Feature: Shipping cost calculation
Rule: Shipping is free for orders over $75; otherwise $5.95 flat rate
Scenario Outline: Shipping cost based on order total
Given an order with a subtotal of <subtotal>
When the shopper proceeds to checkout
Then the shipping charge is <shipping>
Examples:
| subtotal | shipping |
| $50.00 | $5.95 |
| $74.99 | $5.95 |
| $75.00 | $0.00 |
| $75.01 | $0.00 |
| $150.00 | $0.00 |
The boundary ($75.00) is tested from both sides. The rule is stated in prose above the scenarios.
When NOT to apply SBE
Ask two questions before reaching for SBE:
- Will a non-developer ever need to read or validate this specification? If only developers will care, use standard test frameworks.
- Is there genuine ambiguity about what the system should do? If everyone already agrees, write a test and move on.
Skip SBE for: trivial CRUD, pure infrastructure/DevOps, throwaway prototypes, unit-level logic, algorithmically complex internals, and highly exploratory work.
A healthy ratio: SBE scenarios cover 10–20% of the test suite (high-value acceptance layer); unit and integration tests cover the remaining 80–90%.
Reference material
- Collaborative techniques: references/01-collaborative-techniques.md — Three Amigos, Example Mapping (with full walkthrough), Feature Mapping, discovery workshops, facilitation runbook
- Writing effective examples: references/02-writing-effective-examples.md — declarative style, single-behavior rule, detail calibration, ubiquitous language, worked examples
- Lifecycle and living documentation: references/03-lifecycle-and-living-documentation.md — core principles, the seven process patterns, living documentation curation
- Adoption, failure modes, and measurement: references/04-adoption-and-measurement.md — adoption failure modes, maturity model, story slicing with examples, spec-driven development, success metrics
- Glossary: references/05-glossary.md — SBE terminology reference
1---2name: specifying-by-example3description: Writes, reviews, and refines Specification by Example (SBE) artifacts — Gherkin scenarios, example mappings, rule-example tables, and scenario outlines. Produces declarative, single-behavior specifications using ubiquitous language. Use when writing acceptance criteria, Given-When-Then scenarios, feature files, BDD specifications, or executable specifications, when reviewing Gherkin for anti-patterns, when running Example Mapping or Three Amigos sessions, or when the user mentions SBE, BDD, specification by example, or spec-driven development.4---56# Specifying by Example78## Workflow: Writing SBE specifications9101. **Clarify the business goal.** Ask what problem the feature solves and for whom. If the user provides a user story, extract the goal. If they provide raw requirements, ask "why are we building this?"11122. **Identify rules.** Extract the distinct business rules from the requirements. Each rule is a constraint or behavior the system must enforce. Name each rule explicitly.13143. **Generate examples for each rule.** For every rule, produce:15 - The **happy path** (main success scenario).16 - **Key boundary conditions** (edges where behavior changes).17 - **Important negative cases** (what the system should refuse).18 Use realistic data (real names, plausible amounts), not "foo"/"bar."19204. **Write Gherkin scenarios.** Convert each example into a Given-When-Then scenario following these rules:21 - **Declarative, not imperative.** Describe *what* happens, not UI steps. No CSS selectors, button names, or page navigation.22 - **One behavior per scenario.** Each scenario has exactly one When-Then pair.23 - **Essential data only.** Include only data that illustrates the rule. Omit incidental details (user IDs, timestamps, irrelevant fields).24 - **Ubiquitous language.** Use the same terms the business uses. No `user_id`, `POST /api/orders`, or `status = 2`.25 - **Name the rule.** Group scenarios under a `Rule:` keyword in Gherkin.26275. **Use Scenario Outlines for boundary tables.** When a rule has many input-output combinations, use a parameterized `Scenario Outline` with an `Examples` table instead of repeating similar scenarios.28296. **Check for specification gaps.** Are there rules with no examples? Examples with no clear rule? Unanswered questions? Surface these explicitly.30317. **Organize by domain concept.** Group feature files by business capability (e.g., `specs/pricing/`, `specs/accounts/`), not by sprint or ticket number.3233## Workflow: Reviewing SBE specifications34351. Read all scenarios in the feature file or specification document.362. Check each scenario against these anti-patterns:3738| Anti-pattern | Symptom | Fix |39|---|---|---|40| Imperative scripts | Steps say "click," "type," "navigate" | Rewrite in declarative style |41| Multi-behavior scenarios | Multiple When-Then pairs | Split into one scenario per behavior |42| Incidental detail overload | Irrelevant data (user IDs, timestamps) | Strip to essential data only |43| Technical leakage | Steps reference APIs, HTTP codes, DB tables | Replace with business-language equivalents |44| Inconsistent vocabulary | Same concept called different names | Pick one term and use it everywhere |45| Scenario proliferation | Hundreds of trivially different scenarios | Use Scenario Outlines; push combinatorics to unit tests |46| Too abstract | "Given sufficient funds / When they withdraw / Then it succeeds" | Add concrete amounts and expected outcomes |47| UI-coupled specs | Scenarios break when the UI is redesigned | Automate against the API/service layer; use declarative language |48493. Verify the specification covers happy path, boundaries, and negative cases for each rule.504. Propose specific fixes with before/after Gherkin.5152## Quick example5354**Input:** "Users get free shipping on orders over $75, otherwise $5.95 flat rate."5556**Output:**5758```gherkin59Feature: Shipping cost calculation6061 Rule: Shipping is free for orders over $75; otherwise $5.95 flat rate6263 Scenario Outline: Shipping cost based on order total64 Given an order with a subtotal of <subtotal>65 When the shopper proceeds to checkout66 Then the shipping charge is <shipping>6768 Examples:69 | subtotal | shipping |70 | $50.00 | $5.95 |71 | $74.99 | $5.95 |72 | $75.00 | $0.00 |73 | $75.01 | $0.00 |74 | $150.00 | $0.00 |75```7677The boundary ($75.00) is tested from both sides. The rule is stated in prose above the scenarios.7879## When NOT to apply SBE8081Ask two questions before reaching for SBE:82831. **Will a non-developer ever need to read or validate this specification?** If only developers will care, use standard test frameworks.842. **Is there genuine ambiguity about what the system should do?** If everyone already agrees, write a test and move on.8586Skip SBE for: trivial CRUD, pure infrastructure/DevOps, throwaway prototypes, unit-level logic, algorithmically complex internals, and highly exploratory work.8788A healthy ratio: SBE scenarios cover 10–20% of the test suite (high-value acceptance layer); unit and integration tests cover the remaining 80–90%.8990## Reference material9192- **Collaborative techniques**: [references/01-collaborative-techniques.md](references/01-collaborative-techniques.md) — Three Amigos, Example Mapping (with full walkthrough), Feature Mapping, discovery workshops, facilitation runbook93- **Writing effective examples**: [references/02-writing-effective-examples.md](references/02-writing-effective-examples.md) — declarative style, single-behavior rule, detail calibration, ubiquitous language, worked examples94- **Lifecycle and living documentation**: [references/03-lifecycle-and-living-documentation.md](references/03-lifecycle-and-living-documentation.md) — core principles, the seven process patterns, living documentation curation95- **Adoption, failure modes, and measurement**: [references/04-adoption-and-measurement.md](references/04-adoption-and-measurement.md) — adoption failure modes, maturity model, story slicing with examples, spec-driven development, success metrics96- **Glossary**: [references/05-glossary.md](references/05-glossary.md) — SBE terminology reference