Skip to main content
BlogsSoftware Testing guidance

How To Write Gherkin(BDD) Test Scenarios

By December 8, 2022November 29th, 2023No Comments6 min read

‘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?

BDD stands for Behavior-Driven Development. It’s a software development methodology that emerged from the practices of Test-Driven Development (TDD) and Agile software development. BDD focuses on improving communication between different stakeholders involved in software development—such as developers, QA/testers, business analysts, and stakeholders—by using natural language that’s easily understandable by all parties.

Why BDD? 

1. Clear Communication: BDD fosters better collaboration by using a common, understandable language among all stakeholders.

2. User-Focused Development: It emphasizes defining system behaviors from the user’s perspective.

3. Structured Requirements: BDD provides clear, structured requirements using Given-When-Then scenarios.

4. Early Issue Identification: Writing scenarios before implementation helps catch issues early in the development cycle.

5. Automated Testing: BDD allows for automated testing with executable specifications.

6. Improved Collaboration: It encourages effective collaboration among developers, testers, and business stakeholders.

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.

Read also: BDD Pitfalls and Best Practices To Overcome Them 

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. 

Testrig Technologies prides itself on employing renowned testing tools such as Cucumber and Appium, among others, to adeptly create and manage Behavior-Driven Development (BDD) tests, expediting the testing process significantly.

Our proficient QA Testing Services leverage the latest technologies, ensuring expeditious and precise outcomes. Engage with leading QA Company today to discuss your QA concerns and tap into the expertise of our seasoned QA professionals for comprehensive solutions tailored to your needs

Leave a Reply