Given-When-Then
In software testing and behaviour-driven development, Given-When-Then is a structure for writing a scenario in three parts: Given a precondition, When an action happens, Then an observable outcome follows. Gherkin, the plain-language format Cucumber reads, uses Given, When and Then as keywords, and the structure is a common way to state acceptance criteria.
The longer version
A scenario reads like this. Given a customer with an empty basket, When they add a book priced 12.00, Then the basket total shows 12.00. Gherkin adds And and But to extend any of the three steps, groups scenarios under a Feature, and lets a Scenario Outline run the same steps over a table of examples.
Behaviour-driven development, which Dan North originated, brought the form into agile work, and its discipline is one behaviour per scenario. There is one When, Given steps that set up state, and Then steps that check what a user could observe. Steps that mention buttons and selectors tie the scenario to the interface and break whenever it changes, while steps that state the business rule survive a redesign.
The same text can drive automation, since Cucumber and similar tools bind each step to code. That part is optional, and many teams use Given-When-Then only as acceptance criteria on a story, read by a product owner and a tester and never automated. The three steps pay for their extra words where a rule has several cases. A one-line rule such as 'the password must be at least eight characters' is clearer as a plain checklist item.
Common questions
What does Given-When-Then mean?
Given-When-Then is a three-part way to write a scenario. Given sets the starting state, When names the one action taken, and Then states the outcome a user can observe. Together they describe one behaviour of the software.
What is Gherkin?
Gherkin is the plain-text language, with keywords such as Feature, Scenario, Given, When and Then, that Cucumber and similar tools parse. A Gherkin file can be read by non-programmers and run as an automated test once each step is bound to code.
How do you write acceptance criteria in Given-When-Then?
Write one scenario per behaviour, with the starting state, the single action and the outcome a user would see. Use business terms in place of interface elements, and add a further scenario for each distinct rule or edge case.
Is Given-When-Then only used for automated tests?
Many teams use it purely as readable acceptance criteria with no automation behind it, and others bind the steps to code with Cucumber or a similar tool. The format is the same in both cases, and the cost of keeping it in step with the product is not.
Can a Given-When-Then scenario have more than one When?
A scenario should generally have only one When. A single When keeps the scenario to one behaviour, so a failure points at one cause. A chain of actions is usually two scenarios, or a test that has grown into a script of a whole journey.
Bug reports that arrive ready to triage.
Collect replay, console, network and browser context with the Free plan.