BDD - Behavior Driven Development

The Behavior Driven Development is an approach developed by Dan North and Chris Matts. The document is writting using the language Gherkin, a simple language to describe step-by-step the scenario (like a story) and the file has the extension “feature”.

The first step to using the BDD, you need to understand the main problem between members of the project (Client, Manager, Project Owner, Developers, Testers) during the development process: communication. The BDD help us during this process.

The document describe a feature, your conditions, define the result expected, the scenarios and your steps.

The language: Gherkin

The BDD using a domain-specific language to describe the development process, your name is GHERKIN. Using reserved word to group some conditions:

  • Precondition(s): Steps to execute before starting the Main Action;
  • Main action(s): This action results in a behavior used to validate this on Validation;
  • Validation: Validate the behavior(s) generated by the scenario(s).

The reserved words are:

Some special characters used:

Character Description
””” Doc Strings
| Data Table
@ Tag
# Comment

Feature: What it’s

Describe the feature

The feature is a reserved word to define the name of the feature:

Feature: Calculator
  This is a simple description of the feature

Scenario vs Scenario Outline

The scenario is unique or not?

The scenario has the step-by-step to validate an accept story. But pay attention, the data used in the scenario has the possible to reused this to validate. The Scenario and Scenario Outline contains three common phases: Prepare, Execute and Validation.

Scenario

When you need to automate a single scenario, choose this option.

Example:

Scenario: Simple calculator scenario

Scenario Outline

Multiplies scenarios but only the date are changed, choose this option.

Example:

Scenario Outline: Calculator with variation

Step: Given

The preconditions need to start the test.

This step describes the initial conditions before starting the validation step.

Example:

Given I open the calculator

Given I clean the memory

Step: When

Your scenario start here!

This step(s) contains the inputs used during the test

Example:

When I add the values

When I clean the memory

Step: Then

The validation point.

This step(s) start the validation process. Here is the place to verify your goals.

Example:

Then the calculator shows the message "MESSAGE"

Then I receive the result

Step: And & But

Add more information to the scenario

The step AND or BUT is commonly used when you need to add more steps in one o more phases, this steps allows you to create/read a document more fluently.

but remember: the best scenario doesn’t have more than “10” steps.

Example:

And I send the information
But I don't see an error message

Example

The example is the data table used in Scenario Outline, The column represents a variable used on the step and the line represent a script iteration.

Examples:
  This is a simple description
    | COLUMN_X | COLUMN_Y | COLUMN_Z |
    | A        | C        | 1        |
    | B        | D        | 2        |

Background

The background contains the common steps to execute before the scenario(s) in the feature.

Look in the Feature by example and see the Background example.

Improve data and information

DocStrings

When you need to insert/validate a long text, use the DocStrings. Look in the example below:

Then the calculator returns the message
    """
    System error
    """

Data Table

The data table has the same structure as the Example. The data table is used when you need to send some information in the line without describing this information on step (field and data).

Example:

When I add the values
  | valueA          | valueB |
  | 32              | 55     |

Tag

The TAG is a powerfully flag, used to indicate:

  • Identify a special scenario;
  • Set a specific configuration;
  • Precondition(s).

Look at these examples:

# Identify the feature
@calculator
Feature: Calculator

# Identify the scenario
@calculator
Scenario: Access account web
...

# Identify the scenario and the platform
@@calculator @mobile
Scenario: Access account mobile
...

# Identify the scenario on Scenario Outline
Scenario Outline: Invalid access
...
  @bug-2132
  Examples:
    | valueA | valueB | result |
  
  @bug-2135
  Examples:
    | valueA | valueB | result |

Comment

When you need to comment a line or an information block just start the line with “#”.

Examples:

# Comment line

# Comment
# Block
# Instruction

Feature by example


Feature: Calculator

  A simple description of the feature

  Background: Precondition(s) of the scenario(s)
    Given the calculator is open

  Scenario: Simple calculator scenario
    Given I clean the memory
    When I insert the value "2"
    And insert the equation "ADD"
    And insert the value "3"
    And press the equal
    Then the calculator shows the result "5"

  Scenario Outline: Calculator with variation
    Given I clean the memory
    When I insert the value "<VALUE_A>"
    And insert the equation "<EQUATION>"
    And insert the value "<VALUE_B>"
    And press the equal
    Then the calculator shows the result "<RESULT>"

    Examples:
      Scenarios with positive results
      | VALUE_A | VALUE_B | EQUATION | RESULT |
      | 4       | 2       | Add      | 6      |
      | 4       | 2       | +        | 6      |
      | 4       | 2       | Subtract | 2      |
      | 4       | 2       | -        | 2      |
      | 4       | 2       | Multiply | 8      |
      | 4       | 2       | *        | 8      |
      | 4       | 2       | Divide   | 2      |
      | 4       | 2       | /        | 2      |

    Examples:
      Scenarios with negative results
      | VALUE_A | VALUE_B | EQUATION | RESULT |
      | 4       | 0       | Divide   | Error  |

Common troubles

Improper use of the steps

Feature: Wrong scenario

  Scenario: The lost line of thinking
    Given I open the calculator
    And clean the memory
    When I insert the value "2"
    And insert the equation "ADD"
    And insert the value "3"
    And press the equal
    Then the calculator shows the result "5"
    Given clean the memory
    Then the calculator shows the number "0"

List of problems:

  • Use the visual event in the step;
  • Add a visual information on the step description;
  • Confuse logic between steps: When » Given » Given » When » Then

Automation

The main goal behind the BDD is the automation process to execute the feature, for this, you need the choose a framework for this task.

The framework with the “*” has the support the “FEATURE” file:

Java

CSharp

JavaScript

Python

Android

iOS


Author | Henrique Breda

QA Automation Engineer. Passionate about technology and game design.