Ultimate Step-by-Step Guide on Behavior Driven Development (BDD) Testing
In this blog, Devstringx shares a complete guide on behavior-driven development testing, which is the software development process.
Join the DZone community and get the full member experience.Join For Free
Behavior Driven Development (BDD)
Behavior Driven Development( BDD) is a software development process that prioritizes collaboration among engineers and developers. It is a custom software development process that is driven by the behavior of an application and is often called an extension of the Test-Driven Development (TDD) approach.
Why Behavior Driven Development Testing Came Into the Picture
Regardless of the type the application that is to be tested (mobile, web, or desktop), there can be a disconnect between:
- The application is truly able to define the expected outcomes.
- The developer’s understanding of what exactly needs to be built.
- The business( end-user) understanding the technical challenges its requirements may present.
Behavior-driven development can help achieve all of the above and helps a business and its technical team deliver software that fulfills business goals.
Choosing Behavior Driven Development Testing
Test-driven development (TDD) works satisfactorily if the business owner is familiar with the unit test framework being used and their technical skills are good enough, which is not always the case.
In these scenarios, 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 understanding is an advantage to using BDD, making it possible for collaboration between the technical and non-technical teams to run with improved efficiency.
Advantages of using Behavior Driven Development Testing:
- You are no longer just writing “test cases” but are defining the “behavior” of an application.
- Better collaboration between the technical and non-technical teams to run with improved efficiency.
- Easy to learn and implement in the project.
- The behavioral approach defines acceptance criteria, which helps everybody (developer, QA, BA, and business) in the team understands.
Disadvantages of using Behavior Driven Development Testing:
- BDD is not compatible with the waterfall approach.
- BDD is not effective If the user stories are not written perfectly.
- Sufficient technical and programming skills are required for QA as well.
Testing Tools for BDD
Cucumber is the most common and widely used test framework which supports BDD. In Cucumber, the BDD specifications are written in simple English which is defined by the Gherkin language. Gherkin presents the behavior of the application used, from which Cucumber can generate the acceptance test cases.
Specflow is evolved from the Cucumber framework and is used mainly for .Net projects. It also uses the Gherkin language.
Prerequisites for Implementing BDD
- All requirements should be created as a story and each user story should be very well defined.
- Every example used in a user story must be a valid scenario explaining the exact behavior.
- Experience with one of the programming languages (like JAVA, Typescript, Python, Ruby, C#) that will be needed for automation of a test case in the backend (spec File).
Example of a Cucumber/BDD Test:
Feature: Sign up
The user should be able to Sign up. New users should get a confirmation email and be greeted by the site once signed in.
Scenario: Successful sign-up.
Given: I have chosen to sign up.
When: I sign up with valid details.
Then: I should receive a confirmation email.
And: I should see a personalized greeting message.
This is a very basic example – Technical, as well as non-technical people, can understand this. This is the beauty of Gherkin.
Let’s see what each of these keywords is used for:
- Feature: provide a high-level description of a software feature and group-related scenarios.
- Background: add some context to the scenarios in the feature.
- Scenario: This is a concrete example that illustrates a business rule. It consists of a list of steps. Each step starts with Given, When, Then, And
- Given: the initial context of the system – It works like a setup
- When: an event, or an action. This can be a person interacting with the system, or it can be an event triggered by another system.
- Then: an expected outcome, or result. This checks postconditions.
- And: If you have successive Given’s, When’s, or Then’s, you could use And based on the context.
- Scenario Outline: When you need to run the same Scenario multiple times, with different combinations of values.
- Examples: A Scenario Outline must contain an Examples section containing all the different values to run the Scenario Outline with.
- Step Definition: To run the above scenarios in the feature film, a step definition file with a function for each Given, When, Then is required.
Published at DZone with permission of Kamal Singh. See the original article here.
Opinions expressed by DZone contributors are their own.