Gherkin to Code
The Gherkin-to-code workflow starts with a failing scenario, implements minimal step definitions, then builds application code until the scenario passes. This is outside-in development with executable acceptance criteria.
Search across all documentation pages
The Gherkin-to-code workflow starts with a failing scenario, implements minimal step definitions, then builds application code until the scenario passes. This is outside-in development with executable acceptance criteria.
1. Write scenario (fails - no steps)
2. Implement step stubs (fails - NotImplementedError)
3. Build application code (scenario passes)
4. Refactor steps and application together
When to reach for this:
# features/discount.feature
Feature: Volume discount
Scenario: 10% discount for orders over 1000
Given a customer with a cart total of 1500.00
When the discount is calculated
Then the discount amount is 150.00
And the final total is 1350.00# Step 1: failing steps
from pytest_bdd import scenarios, given, when, then, parsers
scenarios("../discount.feature")
_cart = {}
@given(parsers.parse("a customer with a cart total of {total:f}"))
def cart_total(total):
_cart["total"] = total
@when("the discount is calculated")
def calculate():
from myapp.discount import apply_volume_discount
_cart["result"] = apply_volume_discount(_cart["total"])
@then(parsers.parse("the discount amount is {amount:f}"))
def check_discount(amount):
assert _cart["result"]["discount"] == amount
@then(parsers.parse("the final total is {total:f}"))
def check_final(total):
assert _cart["result"]["final"] == total# Step 2: minimal application code
def apply_volume_discount(total: float) -> dict[str, float]:
if total > 1000:
discount = total * 0.10
else:
discount = 0.0
return {"discount": discount, "final": total - discount}What this demonstrates:
apply_volume_discountScenario (acceptance)
-> Step definitions (glue)
-> Service function (domain)
-> Unit tests (edge cases)
myapp/ modules immediately.| Alternative | Use When | Don't Use When |
|---|---|---|
| TDD with pytest | Developer-only design | Stakeholder-readable specs needed |
| Spike then scenario | Unclear requirements | Requirements are clear |
| Example mapping | Workshop output | Already writing Gherkin |
Yes. Red: scenario fails. Green: minimal code passes. Refactor: clean steps and application.
Product + dev together. Developers implement steps and application code.
1-3 acceptance scenarios per user story. Edge cases go to unit tests.
Some IDE plugins scaffold steps from feature files. Manual implementation is fine for small suites.
Fix the scenario first. Scenarios are the spec - code follows.
Use page objects behind step definitions. Gherkin stays UI-agnostic.
All scenarios green, unit tests for edge cases, stakeholders sign off on feature file.
Reuse generic steps with parameters. Limit feature-specific steps to domain language.
Yes on a feature branch. Scenarios document intent before implementation exists.
Green scenarios are the safety net. Refactor application code; scenarios still pass.
Stack versions: This page was written for Python 3.14.0, FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.
Reviewed by Chris St. John·Last updated Jul 19, 2026