Behaviour Driven Development (BDD) was envisaged to combat issues derived from ill-defined requirement specifications and bring business and QA professionals on the same page. In the recent past, there has been a significant shift to Agile development practices by many organizations.
- It delivers better results due to the presence of timely user feedback as well as accelerated delivery cycles.
- The implementation of Agile led to increasing automation in test processes to keep up with the rapid rate of change and adaptability expected from the software development teams.
- And once the pipeline had been optimized enough, software teams paused to consider if they were building the “right” software and not just building it “right”.
Although continuous automated tests ensure optimal test coverage, it is useless if the software fails to align with what the customers have in mind.
This school of thought led to the creation of Behaviour Driven Development (BDD) as an evolution of the Test-Driven Development Methodology (TDD). BDD and TDD testing both follow a sort of “contractual mechanism” to ensure that any output matches a particular expectation.
However, the BDD approach in testing focuses on defining the application’s behavior to make it optimal for business needs rather than the high test coverage-focused principles of TDD.
Table of ContentsWhat is BDD Testing?
Though the term “behavior” might be highly familiar, it acquires a specific technical meaning in this context.
- For BDD Testing, behaviors are a set of well-articulated, human-understandable statements that define a specific process in a predetermined format.
- These are stored in a “feature files” set that is then integrated into the software development process.
- Usually, these files are written in Gherkin, a special syntax used in BDD to allow tools like Cucumber and SpecFlow to automatically validate the “behaviors” encoded for a process.
These BDD Tools parse through the behaviors in the feature file and execute the “glue code” that maps behaviours to executable steps. This sort of mapping is called “step-definitions” or “step-defs” for short.
How does BDD Testing work?
BA/SME teams create Feature files in domain-specific language, and then the Dev, BA, and QAE teams concur on the Feature and Scenarios. QA creates BDD tests for the same scenarios using the same tools and framework as the Dev team.
Every time a build is created, these tests are executed as part of Continuous Integration, and the results are then shared with the entire Sprint team.
Once the client or stakeholders are satisfied with the requirements, Gherkin is used to convert them into Feature files. The feature file is the starting point for the tests, functioning as both a script and a document. It may include a scenario or a list of possible scenarios.
Tools like Specflow and Selenium are sometimes utilized to convert these Scenarios into Step Definitions. Depending on the BDD tool like Cucumber or Behave, every line in the Feature file is transformed into methods, and code can be developed using Selenium and C#. When sufficient Scenarios have been covered, it is deemed deployed for all Sprint teams.
How to write Test Scenarios in Gherkin?
Test Cases and Test Scenarios for the BDD testing framework are envisaged similarly to those written for standard test cases. However, the key difference is in the Gherkin syntax used for articulating them.
Stakeholders initially discuss the product requirements with the developers and testers and decide upon the scenarios that are suitable to be tested. These scenarios are then written as feature files for the automation test tool and are utilized as automated acceptance tests.
The three keywords leveraged for this purpose are –
Given (to provide the context for the scenario) When (to specify the action being performed) And, Then (to represent the desired outcome)
Some further descriptors can be added to this trio to flesh out the scenario even more with the use of And
Given ( some initial context), And (some further context), When (the primary action/event), And (some further action/event), Then (initial outcome) And (further outcome)
Using this logic exhaustive test scenarios can be codified for BDD Testing. Below is a scenario for successful and unsuccessful login to the Browserstack site.
The Need for BDD Testing Framework
Agile Processes and DevOps are primarily concerned with building products in the shortest period possible. However, organizations are wary of pushing out builds that do not fully satisfy customer needs or fail to align with strategic business goals.
- BDD buttresses Agile methodology by allowing constant end-user input to validate the development process and ensure that the “right product” is being deployed.
- It also allows for greater coordination between key stakeholders in the Software Development Process, viz. The Business Team, the Development Team, and the QA Team. These three stakeholders are often called the “Three Amigos”.
Most testing performed using scripting and/or programming languages is not readily perceptible by non-technical stakeholders. Often this leads to a greater onus on QA teams to explain their work in layperson’s terms or, worse, in critical business requirements being misinterpreted.
Leveraging the communicative power of tests written in plain English, BDD software testing allows all stakeholders to be on the same page at all times.
Given the obvious advantages of BDD, several mainstream organisations like BM, Wells Fargo, and CitiBank have become enthusiastic adopters of the technology. Recent market research also predicts a massive increase in the adoption of BDD practices between 2023 and 2029 making it one of the most in-demand frameworks in the industry.
How to perform BDD Testing in Cucumber
Now that a basic introduction to BDD Testing and its importance has been established, it is vital to understand the rubrics of how BDD Testing is performed.
The popular Cucumber- Selenium- Java Framework for BDD Testing is demonstrated for this example.
The scenario is a successful login to the BrowserStack Website.
The Three Major parts of the Cucumber Framework are as follows –
1. The Feature File
A single functionality is defined in BDD as a feature. These features are written in the Gherkin syntax using the Given, When, Then logic, and are stored in feature files with the extension “.feature”
This file stores details about the features, the description of the feature in simple language, the scenarios to be tested, and any pertinent data for testing the feature.
Feature: Login to Browserstack
Description: The user shall be able to login to Browserstack after entering the correct username and password.
As a new user
I want to log in to the Browserstack website
So that I can access the cross-browser testing tools
Scenario #1:
- Successful Log in to the website
- Given A user navigates to the Browserstack site https://www.browserstack.com/
- When A user clicks Sign-in
- And A user enters a valid email <email> and password <password>
- And A user clicks Sign-in
- Then A user should be successfully logged into the site
Scenario #2:
- Unsuccessful Log in to the website
- Given A user navigates to the Browserstack site https://www.browserstack.com/
- When A user enters an invalid email <email> and password <password>
- And A user clicks Sign-in
- Then A user should not be successfully logged into the site
2. Step Definitions
Once the features are set down in the feature files, the code for the scenario needs to be executed.
Step Definitions store the data mapping between each step of the scenario defined in the feature file and the code that needs to be executed to verify it.
Both Java and Selenium commands can be used to map the feature file to executable code.
package StepDefinition; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; public class Steps { @Given("^a user navigates to the browserstack page by opening the browser$") //Code to Open any browser and launch the login page of application to define the GIVEN step of the feature @When("^user clicks on the login button$") //the login button is identified using the find element of the XPATH and is clicked to go to the login page @When("^user enters correct username and password values$") //the inputs for the username and password fields are placed into the correct web elements using XPATH. The WHEN step of the feature is being executed here. @Then (“^user gets directed to homepage$”) //Direct to the Homepage of the application as a result of correct username and password inputs in the WHEN step. This would define the THEN step of the feature }
3. Test Runner File
The Test Runner File is required to run the test. In this case, a JUnit Test Runner Class has the step definition location and the other metadata required to execute the test.
The @RunWith() Annotation in JUnit is required to execute these tests, and the @CucumberOptions Annotation is used to define the locations of the feature files, step definitions, reporting integrations, etc.
For Example:
Test Runner Class in cucumberTest package, with the feature files in “src/test/Feature” location and Step Definition files in “src/main/stepDefinition” folder.
package cucumberTest; import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; @RunWith(Cucumber.class) @CucumberOptions( features = "src/test/Feature" ,glue={"src/main/stepDefinition"} ) public class TestRunner { }
Pro Tip: Browserstack Automate can allow users to run BDD tests on the Cucumber-Java-Selenium Framework on more than 3000+ real devices and browsers.
BDD Testing can help users achieve advanced automation capabilities. Some of the key advantages of its use are –
1- Making test cases more modular by repeating common scenario steps and writing them only once. This is especially useful for writing Regression Testing scenarios.
2- Allowing for Parallel testing to boost test run times. E.g. tools like BrowserStack Automate allow sequential and parallel execution of Cucumber-based Espresso tests. These can be split into groups called shards and executed in parallel to drastically reduce timelines.
3- Allow for comprehensive use of Data Tables to store test data for automated execution. This is again very beneficial for Regression Testing as this allows the scenarios to be tested for various situations.
Feature: I want to login into the site with valid and invalid data Background: Given I navigate to the Website Scenario: Login as a new sign-up user with valid data When I entered a valid credential | email | validpassword | | qatubeupdate@yopmail.com | 12345 | When the user clicks on the sign-in button Then Validate the title after login Scenario: Login with invalid data by entering an invalid password When I entered an invalid credential | email | invalidpassword | | qatubeupdate@yopmail.com | 123456 | When the user clicks on the sign-in button Then Error message should display | errormessage | | Authentication failed |
4- Allow for better test coverage by mapping business requirements in JIRA and other project management tools to test cases within the Feature File itself
User Stories can be integrated by ID into BDD scenarios for directly mapping business requirements to test cases and subsequently into automated test scripts. Cucumber Scripts can map User Story ID from JIRA, as shown below.
Mapping JIRA Requirements in Gherkin
Advantages and Limitations of BDD Testing
Some of the primary advantages of BDD Testing are as follows –
- Fosters Effective Communication and Collaboration by adopting an easy to understand, declarative style of communication to bring technical and non-technical stakeholders on the same page.
- Speeds up Product Delivery by closing the understanding gap between technical and business stakeholders and leading to rapid creation of automated test cases that all the stakeholders can agree upon.
- Reduces human errors in a software implementation by ensuring that the business requirements are hard coded in a readable format for all tests executed.
However, no process is perfect, and BDD also has a few limitations.
- Gives rise to excessively tightly coupled behavior arising from protracted meetings between the stakeholders and the need to include all key stakeholders in the BDD process.
- Requires meticulously prepared software requirements for the Gherkin scripts to effectively articulate the business requirements. This can sometimes be contrary to the rapid pace of Agile teams working off brief specs.
Summary
While Agile processes and Test Automation are key in reducing product delivery timelines and satisfying the demands of an expectant user base, it is crucial to deliver the product exactly as the customers demand.
- BDD testing framework in combination with Agile methods, ensures that the “right product is shipped off in the right manner”.
- Also, its use of natural language scenarios make it ideal for user acceptance testing.
Like with all other tests, it is essential to execute BDD Tests on real browsers and devices for the best results. BrowserStack not only supports BDD Testing on the Selenium Framework, but also provides a Cloud Selenium Grid of 3000+ devices for mobile and web testing.
ncG1vNJzZmivp6x7o77OsKqeqqOprqS3jZympmeXqralsY6wn5qsXZ7Abq7DnWStnaOptq%2Bz