Understanding of the Cucumber BDD (Behavior Driven Development) Framework

Behavioral Driven Development (BDD) Framework:

Behavioral-Driven Development is a framework that bridges technical persons and nontechnical stakeholders by facilitating test cases in simple English. It helps stakeholders to keep track of the project by going through the test scenarios.

Cucumber:

  • Cucumber is the most used Behavioral-Driven Development tool where scenarios are written in simple English, called ’Gherkin’.
  • Three files that are important in cucumber are:
  1. Feature file: A file with .feature extension where test scenarios are written in gherkin language.
  1. Step Definition file: A file with a .java extension where the technical representation of each step written in a feature file is present.
  1. Test Runner file: .java extension file, which acts as a bridge between the feature and StepDefinition file. To run the cucumber suite, we need to run this class.

Feature File:

Keywords that are primarily used in feature files are:

  1. Feature
  1. Background
  1. Scenario
  1. Scenario outline
  1. Given
  1. When
  1. Then
  1. And
  1. Example

Below is the snippet for keywords:

BDD

Difference between Scenario and Scenario Outline:

Both keywords are used to write scenarios. Still, when we want to parameterize a scenario, i.e., if we want to run a scenario multiple times with different test data, then we use a Scenario outline.

Background:

The background is a keyword and kind of scenario that runs before each feature file scenario.

For Example: Suppose there are 4 scenarios in a feature file, and all of them have a common step of logging into the application and navigating to the homepage. Instead of writing that step in all four scenarios, we can remove it from them and keep it as background. Whenever we run a feature file, the background will run before each scenario of the feature file.

Let’s consider a feature of validating the Fashion and Electronics tab functionality in Flipkart. For both functionalities, we need to login and navigate to the homepage.

Feature File:

BDD

Step Definition Class:

In the step definition class, we have methods that contain the implementation of each step of scenarios. Step definition is the coding representation of each scenario step.

Feature File:

BDD

Step Definition:

BDD

Note: The highlighted annotations are used in step definition, which is nothing but the keywords of feature files.

Hooks:

In cucumber, we have 2 hooks, i.e. Before and After.

Before: This hook will run before each scenario of the feature file.

After: This hook will run after each scenario of the feature file.

The below screenshot will demonstrate the implementation of these hooks.

Step Definition Class:

BDD

Console:

BDD

Test Runner Class:

Test Runner class acts as a bridge between cucumber steps and step definition methods. It connects both of them. This class does not have a body and contains annotations like RunWith and CucumberOptions, which help the suite run using Junit.

BDD

Cucumber Options has the below attributes:

  1. Features: for this attribute, we must specify the location of feature files inside the project.

Note: Feature files should be kept inside the maven project's src/test/resources folder.

  1. Glue: The value of this attribute will be the location where the StepDefinition file is kept.
  1. Monochrome: This attribute is related to console output. Work is more readable than the false value if the value is true.
  1. Plugin: This attribute is used to specify the cucumber report format and the location where the report will be generated after execution. We can add multiple report formats at a time. In the above screenshot, no HTML and JSON report designs are added.
  1. Tags: If we have multiple scenarios in different feature files and we want to run specific scenarios as a part of smoke or regression testing, then we will use tags for that selected scenario in feature files and mention those tags as an attribute value in CucumberOptions.

Feature File:

BDD

TestRunner class:

BDD

Conclusion:

Cucumber is one of the best tools as far as BDD is concerned. With its flow, annotations, and keywords it is helpful for testers to code and maintain test cases. It is the best tool for stakeholders like business analysts to understand and track developments.

Subscribe to our email Newsletter

Popular Posts

About the Author

Vikram Raskar

Vikram Raskar

Lead Quality Assurance Analyst

Vikram is an Experienced Lead Quality Assurance Analyst with over 13 years of experience. Vikram has demonstrated a history of working in the information technology and services industry.
Strong quality assurance professional skilled in Agile Testing, SQL, Requirements Analysis, Test Cases, and Test Automation.

How Can We Help You?

Back to Top ↑