BDD Testing Starts Before the Code: Three Amigos
On this page
The most valuable part of BDD happens before a single step definition exists. Teams that skip it and go straight to "write Cucumber tests" usually end up with feature files that read like test scripts and that nobody outside QA ever opens. This chapter covers the conversation that produces good examples, and how those examples become Gherkin.
Discovery, formulation, automation#
A useful way to think about BDD is as three activities, a split popularised by Seb Rose and Gáspár Nagy in their BDD Books series:
- Discovery: the team explores a piece of work through concrete examples and agrees on what it should do. Output: a shared understanding, rules and examples, and a list of open questions.
- Formulation: the agreed examples are written down as Gherkin scenarios, in the language of the business.
- Automation: developers and testers write step definitions so the scenarios run against the system, and keep them passing.
Most teams that "do BDD" only do the third step. The feature files then document whatever the automation engineer thought the feature was, which defeats the purpose. Discovery is cheap (a 25-minute conversation) and finds the misunderstandings that would otherwise turn up as bugs or rework.
The three amigos#
Discovery works best with at least three perspectives in the room, which is why the meeting is often called three amigos:
- Business (product owner, analyst, domain expert): what problem are we solving, which rules matter?
- Development: how could this work, what is hard, what already exists?
- Testing: what could go wrong, what edge cases are missing, how will we know it works?
It is a set of perspectives, not a fixed headcount. A designer, a support engineer or a security specialist joins when the story needs them. One person can bring two perspectives, but it is rare that the person who wrote the requirement also spots its gaps.
A team's testers write all the feature files after the developers finish each story. The product owner has never opened one. What is missing?
Writing Gherkin after the fact is automation without discovery. The examples never helped anyone agree on behaviour, so they are just UI tests with an extra layer. A nicer report does not fix that.
Example mapping#
Example mapping, introduced by Matt Wynne of the Cucumber team, is a simple format for a discovery session. You need four colours of index cards (or a virtual board) and about 25 minutes per story:
| Card | Colour | Holds |
|---|---|---|
| Story | Yellow | The user story being discussed, at the top |
| Rule | Blue | An acceptance criterion or business rule |
| Example | Green | A concrete example that illustrates a rule, placed under it |
| Question | Red | Anything nobody in the room can answer yet |
Here is what a map for the sign-in story used in this course might look like after a session:
STORY A member signs in to reach the secure area
RULE Correct username and password open the secure area
EX Tom signs in with his own password -> secure area, "You logged into a secure area!"
RULE A wrong password is rejected and says the password was wrong
EX Tom with "not-his-password" -> stays on the login page, "Your password is invalid!"
RULE An unknown username is rejected and says the username was wrong
EX "tomjones" (no such account) with Tom's password -> "Your username is invalid!"
QUESTION Is it OK to reveal whether a username exists? (security)
QUESTION Do we lock the account after N failed attempts?
Notice what the map tells you:
- Many red cards mean the story is not ready. Get answers before estimating it.
- A rule with many examples is complex, and may be a story of its own.
- A rule with no examples is not understood yet. "Wrong credentials are rejected" sounds clear until someone asks "rejected how?"
The red card about revealing whether a username exists is a real issue: messages that differ for "unknown user" and "wrong password" let an attacker find valid usernames. The demo site does it anyway, so the course keeps those rules, but in your product this is exactly the conversation that should happen before the code exists.
After 25 minutes of example mapping a story has two rules, three examples and six red question cards. What should the team do?
Lots of open questions mean the team does not yet understand the story. Building it now means guessing, and the guesses become bugs or rework. Questions are not scenarios; they need answers first.
From examples to scenarios#
Formulation turns each green card into a scenario. The rule usually becomes the scenario title or a comment, and the example becomes the Given/When/Then. The first rule above becomes:
Scenario: Tom signs in with his own password
Given Tom has an account
When Tom signs in with his password
Then he is in the secure area
Two things are worth noticing. First, the scenario uses a persona ("Tom") instead of a username and password: the details live in the step definitions, where they can change without touching the scenario. Second, it talks about what happens ("signs in", "is in the secure area"), not how ("types into the username field", "clicks the submit button"). Chapter 2 explains why that matters so much.
Formulation is best done by a pair (often a developer and a tester) right after the session, with the product owner reviewing the result. If the product owner cannot read a scenario and say whether it is right, rewrite it.
When the conversation is enough#
Not every example needs to become an automated Cucumber scenario. Some are better checked by a unit test (for example, the exact rules of a password policy), and some only needed to be discussed. A good rule of thumb: automate as a scenario what the business would want to read as documentation, and test the rest at the cheapest level that catches the bug.