What is BDD?
This is a development method which has evolved from the Test-driven development process. Behavior Driven development is mostly about technical insight and business knowledge.
Why use BDD Framework?
Before the BDD framework, everyone was using TDD. TDD works fine in software development, provided the stakeholders are familiar with the framework being used and their technical knowledge is sufficient. However, this may not be the case always.
BDD provides a path that acts as a bridge to overcome the gap between the technical and the non-technical teams because the test cases are commonly written in simple text, i.e. English. The main advantage of BDD is the plain and clearer approach which is easier to understand.
BDD Format to write test case:
Given – When – Then Approach
- Given: Some given context (Preconditions).
- When: Some Action is performed (Actions).
- Then: Particular outcome/consequence after the above step (Results).
Where this test case written?: in a Feature File
Sample Feature File:
Feature: Login function of Gmail Application
Scenario: As a valid user I should able to Login to G-mail
Given User is navigating to G-mail Login Page
When User need to enter username as "Username" and password as "Password"
Then User is successfully navigated to the G-mail Mail Box
How this Feature file connect with actual test code?:Through Step Defination file.
Example of Step Defination file:
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class Sample {
@Given("^User is navigating to G-mail Login Page$")
public void user_is_navigating_to_G_mail_Login_Page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
}
@When("^User need to enter username as \"([^\"]*)\" and password as \"([^\"]*)\"$")
public void user_need_to_enter_username_as_and_password_as(String arg1, String arg2) throws Throwable {
// Write code here that turns the phrase above into concrete actions
}
@Then("^User is successfully navigated to the G-mail Mail Box$")
public void user_is_successfully_navigated_to_the_G_mail_Mail_Box() throws Throwable {
// Write code here that turns the phrase above into concrete actions
}
}
Reference: Learn End to End BDD framework project integration in Cypress
Advantages Of BDD Framework:
#1) Coverage of User Stories
Due to its easy concept of layman text in the form of feature file allows the stakeholders of technical resources to write the scenarios in Gherkin language using the user stories. The compatibility of the plain text helps to gain maximum coverage on testing.
Feature file containing scenarios are:
- Defined user stories from the business.
- Criteria for the developers to determine if specifications meet business requirements.
- Test scenarios for the testing team.
- Shell cover for an automation tester which allows them to separately write their code in step definition files.
- Explained test scenarios for Stakeholders.
The classification of the step definitions helps the automation tester to keep his code untouched which thereby helps in the maintenance of the scripts.
#2) Clarity of Scenarios
Gherkin language uses plain layman text that is focused on the outcome of the product which is being tested/developed using BDD.
As feature file separate the technical description in a different step definitions file for automation testers, it smartly helps a non-technical person to understand the automated test easily. Any updates can be implemented in a small discussion.
Readability power of gherkin guarantees the clarity of scenarios to each of its user which in turn, helps in building the right product.
#3) Code Reuse in Framework
Given – When – Then approach gives liberty to the testers to use the same steps as many times we want in the feature file which gradually helps in saving time for the automation testers.
Example:
Scenario: Scenario 1
Given User is navigated to Google Home Page
When User searched “Cucumber” in the search engine
Then Clicked on the Search Button
And User can see search results for Cucumber in the web browser
Scenario: Scenario 2
Given User is navigated to Google Home Page
When User searched “Selenium” in the search engine
Then Clicked on the Search Button
And User can see search results for Selenium in the web browser
In the above two scenarios, we can conclude that “Given”, “When” and “Then” steps are reusable in the second scenario.
#4) Parameterization in Feature File
A user can parameterize the gherkin steps in the feature file to obtain reusability in the file.
For Example, if a user is working on a bank application where he logs in to the application again and again. Such kinds of steps could be parameterized with a different set of data and it saves time for the tester. While writing the scenarios, the user has to define the feature file steps in such a way, so that the user can use the common functionality easily.
BDD tools: Behavior Driven Development is a very smart approach in agile methodology. It is always recommended to start either your development or testing using BDD, as using it gives you a platform to work independently with different technologies.
There are lots of tools are available in the market to implement BDD framework such Cucumber, Easy B, JDave, jbehave and so on. But Cucumber is one of the best tools which helps implement the Behavior Driven Development approach in the software project. This allows us to work with many technologies E.g. Java, Python, Jython, etc.
Cucumber language – Gherkin which uses simple plain English words- reduces the communication gap between technical teams and stakeholders and allows them to work together at the same level.