
How To Write Gherkin(BDD) Test Scenarios
‘Keep things simple.’ are the three words you get to hear frequently and more often if you are a developer or a tester. Business problems need simple solutions with better clarity and communication between all stakeholders involved in a project.
The Behaviour-driven-development(BDD) approach to development tries to solve this issue and improves communication between business and technical teams to create software with business value.
This approach leads to a win-win situation for everyone including the business as anyone can see what functionality has been implemented and what is yet to be implemented. Similarly, the developer teams get early feedback so that they can work on broken functionalities and it helps the tester in saving their time by using test automation through BDD.
Although BDD requires you to write test scenarios in plain English language, we need to follow some standards to make it easier for us to reuse the test scenarios as per our needs. This blog will help you understand what the BDD approach is and the way to write test scenarios in BDD.
So let’s begin.
What Is BDD?
Behavior-driven development (BDD) is a software development process to create simple scenarios on how an application should behave from the end user’s perspective.
Why BDD?
BDD ensures that all the stakeholders involved in building the project are in one loop right from the initial stage to the end stage of the project. Implementing BDD helps us automate redundant processes and create structured scenarios by writing the test scenarios in an easily understandable language – Gherkin.
What Is The Gherkin Language?
Gherkin helps us document examples of the behavior our stakeholders want, by using a language that can be easily understood by developers, managers, business analysts, testers, and stakeholders of the team, and by the Cucumber tool. To write test scenarios, all we have to do is deeply understand the keywords used in the Gherkin language and blend in our test scenario based on the Gherkin keywords.
Some Keywords Used In Gherkin Language
• Feature – It is used to describe a software feature, and also to group the related scenarios.
• Scenario – The scenario defines how a system should behave in a particular situation.
• Given – It is used to set up the context where the scenario happens. (Establish context)
• When – It is used to interact with the system. (Perform Action)
• Then – It is used to check the outcome of the interaction. (What we expect).
• And, But – It is used to add more steps to the scenario’s Given, When, or Then section.
How To Write Test Scenarios For BDD
Consider a situation where we need to test the debit/credit card payment page in which a user wants to pay for a particular product or service and proceeds with initiating the payment.
Step 1: Create a Feature you want to verify for your application
In our examples, we are testing the payment page of our application. A feature can have one or many scenarios to describe its behavior.
Feature: Verify Payment Page scenarios
Step 2: Write different scenarios for the feature to be tested
To write a scenario we need to make use of the Given-When- And-Then keywords.
First situation: When the user enters a debit card number but the number is short of the required digits.
To execute this scenario a user enters his debit/credit card number, the expiry date, and the CVV value. As the debit/credit number is short in length the “proceed to pay” button remains inactive.
We can break down this scenario in the Given-When-And-Then format as shown below. Here we expect the user to be on the ‘payment page’ and so we have set the context using the ‘Given’ keyword. To test this scenario a user has to be on the payment page which is a precondition. The user then interacts with the system by inserting data in the debit/credit card field and so we have mentioned it using the ‘When’ Keyword. Following this user action is initiated multiple times which we can state using the ‘And’ keyword. The ‘Then’ keyword is used to define the expected outcome of our test and in this scenario, we expect the button ‘proceed to pay’ to be in an inactive state.
Similarly, we can write a few more scenarios that will test our feature completely.
Scenario 2: When the user enters a valid debit card number of the correct length “proceed to pay” button turns active.
We can write the scenario in this pattern.
Third situation: When the user enters an invalid debit card number of the correct length, the user gets an error message.
We can cover this scenario as shown in the below image.
We can use the ‘Scenario Outline’ keyword to run the same Scenario multiple times, with different combinations of values as shown in the code below.
Pros and cons of using BDD
Pros of BDD
1. Reuse of steps – As the BDD framework uses the Given-When-Then steps, these can be reused between scenarios to save time.
2. Speed up product delivery – BDD helps us to speed up the development process as the modular nature of Gherkin syntax expedites test automation development.
3. Easy automation – BDD frameworks help to easily automate the Test scenarios. As steps are already given in the scenario the tester simply needs to write a function to perform each step’s operations. BDD makes it easier to implement changes in your product.
4. Ease of adaptability – BDD scenarios are broken down at the modular level and are easy to update following product changes. As it is written in the Gherkin language it is easy to edit.
Cons of BDD
1. Needs high Collaboration – To reap the full benefit of the BDD framework we need to have great collaboration between developers, managers, business analysts, testers, and other stakeholders of the team.
2. Need for accurate implementation – It is crucial that the feature files and scenarios are written accurately to yield great results. QA engineers play an important role in the implementation and writing of BDD test scenarios.
Conclusion
So we saw what BDD is and how it is being used to effectively communicate within the team. We also saw how we can write a BDD test scenario using Gherkin that is easily understood by the team and the cucumber tool. Following this, we saw some pros and cons of using the Gherkin test (BDD) method.
At Testrig Technologies, we use popular testing tools like Cucumber, Appium, and others for creating and managing BDD tests that further help speed up the testing process. Our effective QA Testing Services with the latest technologies assure quick and effective results.
Contact Testrig Technologies today and share your QA-related issues to get valuable help from our QA experts.
- - Factors That Led Playwright To Become the Top Test Automation Framework
- - Top Locators In Selenium and How To Use Them
- - Effective Tips To Speed Up Your Test Automation Using Cypress
- - The Benefits of Testing E-learning Application For Superior Digital Education
- - API Testing Postman Vs Rest Assured: Advantages and Challenges
Need a QA Solution? Let’s talk
Find out more about how we can help your organization in creating a defect-free product. Let us know your pain points and we assure to help you address those.
Don't Miss-Out Subscribe!
Sign Up Here To Get the latest Software Testing Tips, Techniques, and Updates Directly to Your Inbox For Free of Cost.
Testrig Technologies,
539 W.Commerce #687,
Dallas TX 75208
+1 (347) 464-1241
info@testrigtechnologies.com
304, Pentagon 3,
Magarpatta city,
Pune, India
+91 2048612088
+91 7219829724
info@testrigtechnologies.com