Given-When-Then
In one line
Given-When-Then is the BDD scenario format: Given some state, When an action occurs, Then this should result.
In simple words
Given-When-Then is a way to describe test scenarios in plain, structured language. It comes from BDD (Behavior-Driven Development) and makes tests readable by everyone — developers, testers, and business stakeholders.
The three parts:
- Given: The starting state or preconditions. 'Given a user is logged in' sets up the initial context.
- When: The action or event. 'When they click the logout button' describes what happens.
- Then: The expected outcome. 'Then they should see the login page' verifies the result.
You can extend with 'And' for multiple conditions: 'Given a user is logged in And they have items in their cart, When they click checkout, Then they should see the payment page.'
This format serves double duty: it's both a specification (readable by business people) and an executable test (tools like Cucumber translate it to code).
Example
// Gherkin syntax (Cucumber)
Given('a user is logged in', () => {
// setup logged-in state
});
When('they click the logout button', () => {
// perform click action
});
Then('they should see the login page', () => {
expect(currentPage).toBe('/login');
});The three clauses map to setup, action, and assertion in the test.
Related terms
Related Resources
Related Lessons
- BDD
Behavior-driven development.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.