Behavior-Driven Development (BDD) Test Automation Using Cucumber

What is BDD

Behavior-driven development (BDD) is a software development process that has come out of the software engineering methodology of test-driven development (TDD).

BDD (behavior-driven development) is largely facilitated through the use of a simple domain-specific language (DSL) using natural language constructs (e.g., English-like sentences) that can express behavior and expected outcomes. Test scripts have long been a popular application of DSLs with varying degrees of sophistication. Behavior-driven development is considered as an effective technical practice especially when “problem space” of the business problem to solve is complex.

GHERKIN

Gherkin is the language which Cucumber understands. It is the Business Readable, Domain Specific Language that lets you describe software’s behavior without detailing how that behavior is implemented.

Behavior-Driven development (BDD) using Cumumber

Cucumber Introduction:

Cucumber is a tool based on Behavior Driven Development (BDD) framework which is used to write acceptance tests for web application.People like Business Analysts, Developers, Testers, etc. Can use this tool for automation of functional validation in easily readable and understandable format (like plain English).

Cucumber feature files can serve as a good document for all. There are other tools also, like JBehave which also support BDD framework. Initially Cucumber was implemented in Ruby and then extended to Java framework. Both tools support native JUnit.

Behavior Driven Development is extension of Test Driven Development and it is used to test the system rather than testing the particular piece of code.

Cucumber Basics:

For the better understanding of cucumber we need to know all the features of cucumber and its usage.

1) Feature Files:

Feature files are essential part of cucumber which is used to write the automation steps for tests or acceptance tests. This can be used as live document. The steps are the application specification. All the feature files ends with .feature extension.

2) Feature:

This let user to know more about the information for the high level business functionality (Refer to previous example) and the purpose of Application under test.By reading the first Feature step everybody should be able to understand the intent of feature file.

3) Scenario:

Basically a scenario represents a particular functionality which is under test. Scenario should be written in such a way that by seeing it the user should be able to understand the intent behind it and what the test is all about. Each scenario should follow or should be in “gherkin” language which is nothing but in given, when and then format.

  1. Given: Given specifies the pre-conditions. It is basically a known state.
  2. When: When specifies the action to be performed. As in below example when the user tries to log in using username and password, it becomes an action.
  3. Then: Then specifies the expected outcome or the result of the action performed. For Instance: Successfull login is the expected outcome or the result after providing USERNAME and PASSWORD.
  4. Background: Background specifies the step that needs to be performed in each scenario. For Instance: If user needs to clear database before each scenario then those steps can be put in background.
  5. And: And is used to combine two or more same type of action.

Example:

Feature: Login Functionality Feature
Scenario: Login Functionality
Given user navigates to SOFTWARETETINGHELP.COM
When user provides Username as “USER”
And password as “password”
Then the user should be able to successfully login
And The user get redirected to the Home page

4) Scenario Outline:

Scenario outlines are used when same test has to be performed with different data set.

5) Tags:

Cucumber by default runs all scenarios in all the feature files. In real time projects there could be hundreds of feature file which are not required to run at all times.

For instance: Feature files related to regression test need not run all the time. So if you mention a tag as regressionTest in each feature file which is related to smoke test and run cucumber test with @RegressionTest tag . Cucumber will run only those feature files specific to given tags.

6) Junit Runner:

To run the specific feature file cucumber uses standard Junit Runner and specify tags in @Cucumber. Options. Multiple tags can be provided by using comma separate. Here you can specify the path of the report and type of report you want to generate.

Implementation of Feature file:

We have to implement these steps in Java in order to test the feature files. Need to create a class which contains those given, when and then statements. Cucumber uses its annotations and all the steps are embedded in those annotations (given, when, then).Each phrase starts with “^” so that cucumber understands the start of the step. Similarly each step ends with “$”. User can use regular expressions to pass different test data. Regular expressions take data from feature steps and passes to step definitions. The order of parameters depends how they are passed from feature file.

Lets talk about BDD automation framework using Cucumber.

BDD automation framework using Cucumber consists of following structure:

  • Feature File : Main controller of framework is “Feature File” which consist of the test cases.
  • Test case : It should be written in Gherkin format that is “Given , When and then”
  • Implementation : In order to test the feature files we need to implement these steps in Java . Need to create a class which contains those given, when and then statements.

Cucumber uses following annnotations(i.e given, when, then) whose steps are embedded in  annotations itself.

Reports :  Cucumber generates its own html format. However better reporting can be done using Jenkins or bamboo tool(CI tools).

Conclusion:

Cucumber is a most favorite tool for many projects as it is easy to understand, readable and contains business functionality.

Nutan Nandanwar

Nutan Nandanwar

Quality Assurance Engineerat Calsoft Inc.
A Software QA Engineer having around 2 years of experience. I have worked on automation testing using different tools like Cucumber, Java, Jira ,etc. in storage domain
Nutan Nandanwar

Latest posts by Nutan Nandanwar (see all)

One thought on “Behavior-Driven Development (BDD) Test Automation Using Cucumber

Leave a Reply

Your email address will not be published. Required fields are marked *