Behaviour Driven Development

Introduction

In this article, we will look at the BDD framework and one tool with respect to BDD framework so that you can use that tool and develop automated Test Script.

Before deep-diving into BDD concepts, Lets us understand what is BDD

Behaviour Driven Development

  • BDD stands for behaviour driven development
  • BDD is all about collaboration between team members
  • It is a collaboration between business and technical team members
  • BDD builds a common understanding regarding the behaviour of the application
  • BDD generates a common document that can be understood by all the stakeholders

For any new requirements we are going to build for the application we go through the following process

  • Discussion is done among the different team members.
    • discussion on how system should behave
  • Examples are created
  • Examples are documented in a way that can be developed and tested
  • Coding phase

Three phases of BDD,

  1. Discovery
  2. Formulation
  3. Automation

Discovery phase

Explore and discuss within the team on how the system should behave

The output of this phase is the agreed system behaviour

Formulation phase

Create concrete examples that can be automated

The output of this phase is Acceptance tests

Examples are created using a feature file, which is written using gherkin language.

Automation phase

Coding the documented examples and implementing the agreed system behaviour.

The output of this phase is automated code.

Tools which helps us to write test cases in bdd are,

  • jbehave
  • cucumber
  • specflow
  • Fitnesse

While understanding BDD we should know the other techniques used before BDD came in to existence,which are TDD and TLD

Test Last development

TLD takes place after the coding, so analyzing and brainstorming will be the first step, then writing the code, then maybe refactoring, and the last thing will be writing unit tests and making them work.

Test Driven Development

Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against all test cases.

So BDD is the extension of the TDD.

Now let's look in detail regarding cucumber Tool.

Cucumber is BDD tool which helps testers to write automated tests in BDD style.

Cucumber is written in ruby language.

Cucumber can be used to "test" scripts written in Ruby or other languages like Java, C# and Python.

In order to write tests in cucumber you need three components

  1. A Runner file
  2. Feature files
  3. Step Definition.

Runner File

Runner file is a java file which helps us to run the cucumber tests depending upon the cucumber options set.

Feature option

In Runner file we need to refer the path for the feature file which you have written.

Glue option

In Runner file we need to refer the path for all the step definition files.

DryRun

This option checks if all the steps in the feature file has a step definition method linked to it.

Monochrome

After running your cucumber test you will see some unicode characters in the console log. In order to remove those we need to set this monochrome option to true so that output in console in readable manner.

Strict

This option if set to true then while running the runner file then cucumber will check if there are any step not linked with the step definition or any undefined steps are present.

Tags

Tags helps us to execute certain tests to be executed. Eg, if you have smoke test and named as tag then we can execute the set of smoke test with help of tag.

Format

It is the report which we want to configure in order to view the tests result. It support XML, HTML and JSON reports. We need to configure in this step.

Example of the runner file below,

package cucumberTest;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "Feature", glue = {
    "stepDefinition"
}, dryRun = true)
public class cucumrunner {

Feature File

This is a file where you will describe your tests in Descriptive language (Like English). We write feature file using gherkin language.

This is a very important component of the cucumber.

Feature file can contain one or many scenarios.

If you are developing a cucumber automation project using java, In eclipse there is a plugin called Natural plugin which helps us to write feature files.

Some keywords used in Feature files

  • Feature - Feature describes the functionality of the test you are going to write.
    Eg: Feature: Add to cart
  • Background - Background keyword is used to define steps that are common to all the tests in the feature file.
  • Scenario - Each Feature will contain a number of tests to test the feature. Each test is called a Scenario and is described using the Scenario
  • Scenario Outline - If you want to execute the same Scenario with various combinations of values or arguments, one could use the Scenario Outline.
  • Given - Given defines a precondition to the test
  • When - When keyword defines the test action that will be executed.
  • Then - Then keyword defines the Outcome of previous steps
  • And - And keyword is used to add conditions to your steps.
  • Background - Background is used to define series of steps that are common to all the tests in the feature file.

Examples

The multiple arguments passed against the same scenario outline are called Examples.

| Username | Password|
| test     | test@12 |
| testtwo  | test@13 |

Example of a feature File,

Feature: Login Action

Scenario: Successful Login with Valid Credentials
	Given User is on Home Page
	When User Navigate to LogIn Page
	And User enters UserName and Password
	Then Message displayed Login Successfully

Step Definition

Step Definition is a  method in a class with an annotation above it. An annotation followed by the regex pattern. Step Definition links all the Steps in feature file, and this code is what Cucumber will execute when it sees a Gherkin Step. Cucumber will find the Step Definition file with the help of the Glue code using Cucumber Options.

Summary

This article will help you to understand BDD framework in detail and what are the terminologies that are used in BDD Framework


Similar Articles