1 / 10 · Book 11 · Anatomy of a Test Case · drill: interview Q&A⊞ allnext →Get the book →
1.3The Given/When/Then Template
GIVEN [precondition / system state]
WHEN [user action or trigger]
THEN [expected observable result]
This format, from BDD (Behavior-Driven Development), works even outside Cucumber frameworks. It forces you to separate setup from action from assertion.
Examples
GIVEN a registered user with an active account
WHEN the user enters valid email and password and clicks Sign In
THEN the user is redirected to /dashboard and sees a welcome message
GIVEN a shopping cart with 2 items totaling $50.00
WHEN the user applies discount code "SAVE10" (10% off)
THEN the cart total updates to $45.00 and the discount line is visible
GIVEN a user with no upload permissions
WHEN the user attempts to upload a file via the API
THEN the API returns HTTP 403 with message "Insufficient permissions"
When to Use Each Format
| Scenario | Recommended Format |
|---|---|
| Agile team using BDD / Cucumber | Given/When/Then (mandatory) |
| Quick internal test documentation | Given/When/Then (concise) |
| Formal test plans for regulated industries | Traditional table format (detailed) |
| Exploratory testing charters | Neither — use free-form charters |