If you’re like me, you might have heard about Behaviour Driven Development (BDD), but have struggled to understand how to write your Acceptance Criteria in this format. Agile training courses encourage using it, but rarely offer an explanation of how it works in practice.
It wasn’t until I was using BDD on a daily basis within a team that had already adopted the practice, when it all clicked for me. This is why I’m aiming to explain in practical terms how it works, so that you can either begin using it or get a refresher on BDD.
BDD aims to explain the behaviour of a system in easy to understand language1. Using BDD and Scenarios allows requirements and tests to utilise the same approach in documenting system behaviour. In some cases, the Scenarios that are written can be converted into Automated Tests.
The format is fairly simple, but sometimes hard to get your head around how to write in this format. In order to explain how it works in practice, I will be using the Facebook registration page (shown below) as an example within my Scenarios2.
Given… When… Then…
In its simplest format, BDD uses three main descriptors to write a Scenario: Given, When, Then. The text that follows after the descriptors is explained below:
Given [context]
When [event]
Then [outcome]
Taking the Facebook example, below is a scenario that goes through the sign-up process:
Given Fred is signing up for Facebook,
When he submits the required details,
Then a Facebook account is created.
Using Ands
When you require more information in a scenario, an ‘And’ can be used after any of the descriptors:
Given [context],
And [more context],
When [event],
And [another event],
Then [outcome],
And [another outcome].
What I found was lacking in my Facebook scenario was the fact that it assumed that Fred had entered the details. I’ve also added a bit more detail of what happens when Fred’s account is created.
Given Fred is signing up for Facebook,
When he enters the required details,
And submits his request,
Then a Facebook account is created,
And account name is set as Fred’s email address,
And a confirmation email is sent to Fred.
Of course, then the question could be asked, what are the required details that Fred needs to enter? Below I have used multiple ‘Ands’ to explain all the events that are required to be met before an account can be created.
Given Fred is signing up for Facebook,
When he enters his First Name,
And enters his Last Name,
And enters his Email,
And re-enters his Email,
And enters a new Password,
And specifies his Gender,
And enters his Birthday,
And submits his request,
Then a Facebook account is created,
And account name is set as Fred’s email address.
And a confirmation email is sent to Fred.
Using tables to summarise variables within a Scenario
As you can see the above example is quite long winded. A technique that can be used to summarise the multiple ‘Ands’ within your scenario is to use a table (or a bullet point list as I’ve used below). When using this format, all of the variables within the table must be met in order for the Scenario to be successful. Below is an example of how to do this:
Given Fred is signing up for Facebook,
When he enters the following required details:
Required details
- First Name
- Last Name
- Confirm Email
- Password
- Gender
Birthday
And submits his request,
Then a Facebook account is created,
And account name is set as Fred’s email address.
And a confirmation email is sent to Fred.
One thing to be careful of when using this format is to consider whether your scenarios are going to be converted into Automated Tests. If this is the case, then depending on the tool you’re using the formatting is important, otherwise it will break the build.
One final thing…Adding a title to your Scenario
Each of your Scenarios for the Story should have a title. The title explains what the activity is that the user (in this case Fred) will be able to complete when we finish the scenario. This way you will have an idea of what the finished Scenario should look like before you start3. Below I have provided an example Title for the Facebook scenario I have been elaborating:
Scenario: 01: Completing sign up form creates Facebook account
Given Fred is signing up for Facebook,
When he enters the following required details:
Required details
- First Name
- Last Name
- Confirm Email
- Password
- Gender
- Birthday
And submits his request,
Then a Facebook account is created,
And account name is set as Fred’s email address.
And a confirmation email is sent to Fred. Although this is all I will go through today, I will be continuing to look into how to use BDD in a few weeks time. This will include how to write exception scenarios, and splitting out Scenarios into different Story Cards. If you’re looking for some further information in the meantime, you can refer to the references I used while writing this article.
Credit must also go to Paul Velonis and Rupi Parmar who provided a few tips when writing this article.
References: