What is BDD?
Behavioral Driven Development (BDD) is a software development approach that has evolved from TDD (Test Driven Development). It differs by being written in a shared language, which improves communication between tech and non-tech teams and stakeholders. In both development approaches, tests are written ahead of the code, but in BDD, tests are more user-focused and based on the system’s behavior.
Choosing BDD
TDD works satisfactorily, as long as the business owner is familiar with the unit test framework being used and their technical skills are strong enough, which is not always the case. In these circumstances, BDD has the advantage because the test cases can be written in a common language used by the stakeholders such as, for example, English. This access to clearer, low-jargon communication is probably the biggest advantage to using BDD, making it possible for collaboration between the technical and non-technical teams to run with improved efficiency.
Characteristics of BDD
As described above, the advantage to BDD test cases being written in a common language is that details of how the application behaves can be easily understood by all. For example, test cases can be written using real-time examples of the actual requirements, to explain the behavior of the system.
Essentials to have in place before implementing BDD
- Requirements should be converted into user stories that can define concrete examples.
- Each example should be a valid user scenario, rather than a mere test case.
- An understanding of the ‘role-feature-reason’ matrix and the ‘given-when-then’ formula.
- An awareness of the need to write ‘the specification of the behavior of a class’ rather than ‘the unit test of a class’.
The ‘Given-When-Then’ formula BDD example
This is the proposed template for writing BDD test cases for a user story, which can be defined as:
Given a certain scenario
When an action takes place
Then this should be the outcome.
A practical example would be:-
Given the User has not entered any data on the form
When they click the submit button
Then proper validation messages should be show.
Role-Feature-Reason matrix
This template is also used in BDD, in order to aid user story creation. This template is defined as:-
- As a
- I want
- So that
An example of such a user story would be: – As a retail customer, I want to return an electronically purchase merchandise within 14 days, so that the refund will be processed
BDD Tools
Cucumber is a test framework that supports BDD. In Cucumber, the BDD specifications are written in plain, simple English which is defined by the Gherkin language. In other words, Gherkin is a language that Cucumber understands. Gherkin presents the behavior of the application used, from which Cucumber can generate the acceptance test cases. Cucumber is a framework developed by Ruby that can work across different technologies.
Specflow evolved from the Cucumber framework using Ruby on Rails, and is used mainly for .Net projects. SpecFlow also uses the Gherkin language.
Some benefits to using BDD
If you plan to implement BDD, here are a few points that will benefit the software team.
- You are no longer defining ‘test’, but are defining ‘behavior’.
- Better communication between developers, testers and product owners.
- Because BDD is explained using simple language, the learning curve will be much shorter.
- Being non-technical in nature, it can reach a wider audience.
- The behavioral approach defines acceptance criteria prior to development.
Disadvantages of BDD
Even the best development approaches can have pitfalls and BDD is no exception. Some of them are:
- To work in BDD, prior experience of TDD is required.
- BDD is incompatible with the waterfall approach.
- If the requirements are not properly specified, BDD may not be effective.
- Testers using BDD need to have sufficient technical skills.
Summary
BDD lets us develop, test and think about the code from the view of the business owner. You need to have the mindset to implement ‘real time examples’ rather than implementing just ‘functionalities’ So try it out!