• info@helpingtesters.com
  • helpingtesters

BDD Testing With Cucumber

March 12, 2017
bdd test, cucumber testing, cucumber tool, cucumber testing tool

Cucumber is a framework available to write and test the application by using the high-level description of the software. It is a tool which is used frequently in projects which use Agile processes. It bridges the gap of communication across teams, reduced the workload of documentation and provides us with a common language which every stakeholder speaks and understands. Rather one of the main benefits of cucumber is that it allows a common collaborative document in which the application and end user behavior are captured. This document can be used by any team member and it will provide all the relevant information.

Component of Cucumber Framework

The cucumber framework has two important components

  • Business Component
  • Technical Component

The following diagram depicts the representation of the components

bdd test, cucumber testing, cucumber tool, cucumber testing tool

The cucumber framework supports the concept of DRY, which stands for do not repeat yourself. So in this, it is possible that we have repeated business lines, but the code supporting them is same. So we do not write duplicate code as this is not supported rather prohibited by the framework itself.
The business component of cucumber is made up of Feature files. And the technical component of cucumber is made up of scripts called as step definition files which are written in a programming language like ruby, java etc.

Language of Cucumber Framework

The domain specific language or the DSL of the cucumber framework is Gherkin. It is using this language that the cucumber files called as ‘Features’ are generated. In these files, the behavior of the system and the user is captured. The gherkin keywords of feature files are as follows:

  • Feature
  • Background
  • Scenario
  • Scenario Outline
  • Given
  • When
  • Then
  • Examples

 

A sample gherkin feature file example is as follows:

Explanation of the various components of the gherkin DSL

Feature

Every gherkin file begins with the feature keyword. It describes the functionality of the application under test. E.g. Login Functionality of the application.

Background
This keyword is used mainly when we have also used ‘Examples’ keyword in the file. It acts as a system resetting option before every iteration run, as mentioned in the example section.

Scenario
These are the tests which are available to test a particular feature. For example ‘Login’ can be a feature of the application, but to test it for login with valid credentials, and to test for login with invalid credentials these will be the scenarios available with the feature files.

Scenario Outline
Instead of scenario, we use scenario outline when we want the tests to run in iterations. In such situations, the keyword scenario outline is used.

Given
Given keyword is used to define a pre-condition. It is used to tell that the system is in a particular state, or the system is set in a particular state and is ready for action. E.g. Given the user has valid credentials and we are on the home page of the application.

When
The when a keyword is used to define the behavior system generates and the user generates to run the tests. It is in this keyword that the actual test steps execute. E.g. When user clicks on my account link and user types email and types password etc

Then
The then keyword is used for validation of the action defined in the ‘when’ keyword. It is like a post condition. We wish to test the state of the system as per expected. E.g. Then the user should be able to access the account. Etc.

Examples
We have the examples keyword to display the data which is going to be used in the iteration of the scenario during the time of execution.

Cucumber framework and Testing

Cucumber framework provides us with a ready to automate approach, as well as documentation when we use this framework to write down our test cases. We need to understand that there is a shift in focus required when we write down tests using BDD cucumber than the traditional test case writing approach. Example let us take the scenario for facebook logging

Traditional Manual Test Case
The traditional manual test case will be as follows:

  • Open the application URL – http://www.facebook.com
  • Type the email address in the email id field
  • Type the password in the password field
  • Click on the login button
  • Verify you can see the logoff link on the page
  • Click on the log offline

BDD Scenario

Feature: Login
Scenario: Valid user login
Given: the user has valid credentials and we are on the home page of the facebook application
When we type in the email address xyz@xyz.com
And we type the password “12345”
And we click on the login button
Then we see the logoff link on the page
When we click on the log off link
Then we are back on the home page of the application.

As you will see in the above two approaches, the BDD file provides you with a wholesome picture and clear information about the use case. This document any stakeholder can understand, update and consume. Moreover, when we generate the script file with the corresponding file, known as the step definition file we will have 1-1 mapping available for every user action as mentioned in the feature file.

Advantages of the Cucumber Framework

There are various advantages of the cucumber framework

  • A collaborative document which can be used across stakeholders in a team.
  • Reduce documentation workload.
  • Usage of Gherkin language which a human mind can easily decipher.
  • Support for around 40 languages to generate the feature file.
  • Support available with multiple programming languages like Ruby, Java, Python etc to implement it for automation.

As with the era of Agile processes, we are going to find more and more projects and test automation tools shifting to the BDD Cucumber approach to testing and product development. To learn about it more, join our Cucumber Online Training – Cucumber Automation Testing With Java course today.

About the author

Leave a Reply

Your email address will not be published.