Behavior Driven Development (BDD) Testing

What is BDD?

Behavior-Driven Development (BDD) is a software development technique for automated testing and it also can be successfully applied in Agile or other software development models. It focuses on the language and interactions used in the process of software development. Instead of a business stakeholder passing requirements to the development/test teams without much opportunity for feedback, the developer, stakeholder and tester collaborate to write acceptance tests that express the outcome that the stakeholder wants. The acceptance tests express what the software needs to do in order for the stakeholder to find it acceptable. BDD practitioners work from the outside-in, starting with a failing acceptance test that describes the behavior of the system from the customer's point of view. Acceptance tests are written as examples that anyone on the team can read. Acceptance tests also encourage to get feedback from the business stakeholders about whether we are setting out to build the right thing before we get started on the development. A shared ubiquitous language is developed for talking about the system as acceptance tests. Then those acceptance tests are automated using one of the BDD tools mentioned in one of the sections below to provide quick and continuous feedback to the team.

Ubiquitous Language

A Ubiquitous language is used and understood by everyone involved in the project. When the team uses this language consistently in their conversations, documentation, and code, the friction of translating between everyone's different little dialects is gone, and the chances of misunderstandings are greatly reduced. Acceptance tests are written in this language that business stakeholders can understand. By collaboratively working to write these tests, not only do the team members decide what behavior they need to implement next, but they learn how to describe that behavior in a common language that everyone understands.

When we write these tests before development starts, we can explore and eradicate many misunderstandings long before they ooze their way into the code base. Acceptance tests written in this manner can easily be read and written by anyone on the team.

Gherkin

Gherkin is an Ubiquitous language for BDD. It enables to express the acceptance tests in plain English. Gherkin syntax is used when writing BDD specifications. This syntax is readable by anyone and it is domain specific language that allows you to describe the behavior of the system and not specifying the details of how that behavior is implemented. This allows using real-world examples to describe the desired behavior of the system we want to build. 
It serves two purposes:

  • Documentation - The BDD acceptance tests can fulfill Design Control documentation as they contain acceptance criteria.
  • Automated Tests - The tests will be automated and this serves as automated acceptance tests."

GivenWhen and Then are the main BDD keywords:

  • Given step represents a precondition to an event
  • When step represents the occurrence of the event
  • Then step represents the outcome of the event

Acceptance tests written in gherkin format will serve as Requirements and will read like documentation.

Typical BDD Steps within a Program

  1. In software programs using Agile, typically Product Owner (PO) writes stories.
  2. PO/Tester writes BDD scenarios in collaboration with Development Architect/Tech Lead
  3. Tester/Developer writes automated acceptance tests for scenarios using one of BDD tools
  4. Tester/Developer runs test and watch it fail
  5. Developer writes production code using TDD
  6. Run automated acceptance tests until they pass so story can be accepted as complete
  7. Move to the next scenario/story

If programs don't follow agile software development, Product Manager in collaboration with engineering shall provide enough input based on which BDD scenarios can be written. Decision shall be made by the team who will be writing BDD scenarios

BDD Process

BDD is a two-stage process. The first stage is to write Acceptance Tests in the form of BDD scenarios using Gherkin syntax in collaboration with business stakeholder, developers and testers. The second stage is to automate those acceptance tests created in step one using one of the BDD automated tools. This will ensure that we are applying the best technical practices to build the product right.

Writing BDD Specifications

Acceptance tests are written using Gherkin syntax in the form of Given/When/Statements. The keywords that are used to define acceptance tests are:

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

Examples

Feature: Login to application

Scenario 1: User logs in with valid credentials

Given login screen is displayed on trying to access the application 
When I enter valid user credentials
Then I should see viewer page

Scenario 2: User logs in with invalid credentials

Given login screen is displayed on trying to access the application 
When I enter invalid user credentials
Then a message should be displayed saying that invalid user credentials have been entered

BDD Automation

The second stage in the BDD process is automating the acceptance tests written in Given/When/Then format. The BDD automation can be performed at the API/Services/Web Services and at the User Interface (UI) layers of the application.

BDD Automation Tools


The figure below shows recommended BDD tools to be used to automate testing for each layer within a typical application. The sections that follow provide more details about each recommended tool as well as some examples.

SpecFlow – BDD tool for .Net


SpecFlow is a BDD tool to test .Net based applications. It aims at bridging the gap between technical and non-technical stakeholders by binding the acceptance tests written in Given/When/Then statements to the underlying implementation. 
Example:
Step 1: Describe the behavior of the system in the form of BDD scenarios using Given/When/Then statements
Step 2: Automate the scenarios to establish validated living documentation 


SpecFlow Example

SpecFlow Workflow

JBehave – BDD Tool for Java

JBehave is a BDD automated test tool for Java based application. It uses plain text Gherkin syntax. It maps the scenarios in Gherkin to steps in Java using annotations.

JBehave Example

JBehave Workflow

 

posted on 2017-10-16 15:53  swee632  阅读(668)  评论(0编辑  收藏  举报

导航