Writing Good Gherkin Scenarios
Good Gherkin is the heart of successful BDD. Bad Gherkin leads to unreadable tests, high maintenance, and loss of business trust.
This section explains how to write clean, meaningful, and maintainable Gherkin scenarios.
Purpose of a Gherkin Scenarioβ
A scenario should:
- Describe one business behavior
- Be understandable by non-technical users
- Act as acceptance criteria
- Be executable as an automated test
Rule:
If it reads like test steps, not behavior β itβs bad Gherkin.
One Behavior per Scenario (Golden Rule)β
Each scenario should validate:
- One outcome
- One business flow
β Bad:
- Login + Payment + Logout in one scenario
β Good:
- One scenario for login
- One scenario for payment
Use Business Language, Not UI Languageβ
β Bad Gherkinβ
When user clicks submit button
Then user sees dashboard page
β Good Gherkinβ
When user submits valid credentials
Then user should be logged in successfully
Avoid Technical Detailsβ
Do NOT include:
- Selenium actions
- XPath / CSS
- API endpoints
- SQL queries
Gherkin is WHAT, not HOW.
Keep Scenarios Short and Focusedβ
Best practice:
- 5β8 steps per scenario
- Clear flow
- No unnecessary details
Long scenarios = poor maintainability.
Use Consistent Vocabularyβ
- Use same wording across features
- Avoid synonyms for same action
- Helps step reuse
Example:
- Always use βlogs inβ instead of βsigns in / enters / authenticatesβ
Avoid Overusing And / Butβ
Use And for readability, not to hide complexity.
β Bad:
Given A
And B
And C
And D
And E
If too many Ands β scenario is too complex.
Write from Userβs Perspectiveβ
Think like:
- User
- Business
- Product owner
Not like:
- Automation engineer
- Developer
Good vs Bad Scenario Exampleβ
β Bad Exampleβ
Scenario: Login test
Given browser is opened
When user enters username
And user enters password
And user clicks login
Then dashboard page should load
β Good Exampleβ
Scenario: Successful login with valid credentials
Given user has a valid account
When user logs in with valid credentials
Then user should see the dashboard
Common Gherkin Smells ββ
- Too many steps
- UI-centric language
- Technical jargon
- Duplicate scenarios
- Hardcoded test data
Interview-Ready Questionsβ
Q: What makes a good Gherkin scenario?
A: Business-readable, single behavior, no technical details.
Q: Should UI actions be written in Gherkin?
A: No, Gherkin should describe behavior only.
Key Takeawaysβ
- Gherkin should read like acceptance criteria
- One behavior per scenario
- Avoid UI & technical language
- Consistency improves reuse
- Good Gherkin reduces maintenance