What is Behaviour-Driven Development? (BDD)
What is Behaviour-Driven Development?
Behaviour-Driven Development (BDD) is a process that focuses on how your product should behave. It can be used in any software development project, and it focuses on:
What: What is the behaviour you want to achieve – for example, “when a user clicks the ‘submit’ button, we want them to see a message stating they submitted successfully.
Why: The why is why you want this behaviour – for example, “to improve customer experience by reducing error rates”.
How: The how is how you plan to implement this behaviour – for example, “by adding success message after submitting”.
Persona: The persona represents a particular group of users who would use this product – for example, “power users”.
Where to start in the process
What to test and what not to test
How much to try in one go
What to call the tests
How to understand why a test fails
How does BDD work?
See below for an example of what BDD looks like when applied to an app.
When I enter my email address in the login screen…
Then I should see “You are logged in”
Another example of a BDD User Story.
An explicit title.
A short introductory section with the following structure:
- As a: the person or role who will benefit from the feature;
- I want: the feature;
- so that: the benefit or value of the feature.
A description of each specific scenario of the narrative with the following structure:
Given: the initial context at the beginning of the scenario, in one or more clauses;
When: the event that triggers the scenario;
Then: the expected outcome, in one or more clauses.
Here is an example of a BDD actual test scenario.
Title: Returns and exchanges go to inventory.
As a store owner,
I want to add items back to inventory when they are returned or exchanged,
so that I can track inventory.
Scenario 1: Items returned for a refund should be added to inventory.
Given that a customer previously bought a black sweater from me
and I have three black sweaters in inventory,
when they return the black sweater for a refund,
then I should have four black sweaters in inventory.
Scenario 2: Exchanged items should be returned to inventory.
Given that a customer previously bought a blue garment from me
and I have two blue garments in inventory
and three black garments in inventory,
when they exchange the blue garment for a black garment,
then I should have three blue garments in inventory
and two black garments in inventory.
BDD helps your team stay focused on what matters most when developing software. It encourages collaboration between various disciplines, emphasising empathy towards your customers. This means that when developing software, everyone involved will be thinking about their target demographic instead of just themselves.
How does BDD work?
BDD starts with a ‘Gherkin’ script. This script is written in a natural language, and the intention is to have the programmer read it and understand what is being asked of them.
For example, a Gherkin script could be:
“As a power user, I want to be able to create a task when I’m logged in.”
Then, the programmer would read that sentence and know that they need to add an option when the power user logs in to create a new task.
The next step of BDD would be for the programmer (or someone else) to write another Gherkin script of how they plan on implementing this behaviour:
“When I log in as user X, I should see the ‘create new task’ button.”
This second Gherkin script describes how they will implement the first Gherkin. They can then get all their programmers together and get started on coding.
Who should use BDD?
BDD is an excellent idea for anyone who wants to test their product before releasing it. It is perfect for people who want to create something that has never been made before or those trying to solve a problem that can be solved in many ways.
If you want your product tested by an outsider, BDD is perfect because it focuses on what the customer will know and do (the personas). What they think doesn’t matter; they can’t think of everything after all.
BDD should be your go-to strategy if you’re writing code because it focuses on how the code behaves. This way, if there are any bugs or errors, they will show up during testing.
Tips for successful BDD implementation
1. Identify the personas of your users.
2. Specify what you want them to do.
3. Estimate how you plan to implement this behaviour.
4. Estimate how long it will take to implement this behaviour.
5. Write a scenario specifying the above points
Behaviour-driven development is a popular technique for testing both the functional and non-functional aspects of software development. Using a framework such as Cucumber or JBehave, your team can write tests in a clear, readable language describing how a user interacts with the system. These tests can then be easily understood by both developers and stakeholders and can even generate documentation automatically.
Using behaviour-driven development in your project will help you deliver a better product aligned with your customer’s needs. You’ll also have a robust set of automated tests that will help you confidently deploy your software, knowing it will work as expected.
BDD Source and Further Reading
- Behaviour-driven development – Wikipedia – Wiki
- Cucumber – Official Documentation
- Skillshare (1 Month Free) – Training
- The Cucumber Book 2e: Behaviour-Driven Development for Testers and Developers – Book
- Mastering Behavior-Driven Development Using Cucumber: Practice and Implement Page Object Design Pattern, Test Suites in Cucumber, POM TestNG … and work with Selenium Grid – Book.