BDD: A cucumber recipe
Trend of BDD
Agile methodology is an incremental and iterative software development methodology. It tries to satisfy the customer through early and continuous delivery of valuable software, welcoming changes in the requirements even in the later stages of development. This methodology has been popular these days. Scrum and Extreme Programming (XP) are two well-known agile methodologies. However, XP and Scrum necessarily have two processes in play on agile teams addressing different aspects of software production. The user stories practices in Scrum represent analysis and specification in agile projects and Test-Driven Development in XP represents software design and software quality.
Behavior-Driven Development (BDD) comes out of the friction between these two processes which tightens the communication and feedback across the gap between XP and Scrum. Behavior-driven development is about implementing an application by describing its behavior from the perspective of its stakeholders. BDD makes use of user stories. Developers use user stories to capture requirements and to express customer expectations and are used to plan and to track progress. User stories express requirements in terms of The Role, The Goal, and The Motivation. It extends TDD by writing test cases in a natural language that non-programmers can read. They are used for both the design and the testing of the software. BDD uses the concept of modeling a system using a ubiquitous language based on the business domain. The stories written are the acceptance criteria for the system and also act as the documentation of the software. BDD has evolved out of established agile practices and is designed to make them more accessible and effective for teams new to agile software delivery is gaining popularity.
Cucumber is very interactive testing tool where features or requirements are written in plain text in native language. It complies with Domain Specific Language for implementing BDD technology. It provides automated test and self documentation which provides easy way to communicate with clients and developers. It works with ruby, java, .net and many other web application languages. We will be discussing about its implementation using ruby language.
Here is an example of the feature file we have implemented in a simple doctor and department managing application.
Feature: Manage Doctors
In order to save doctors information
As an administrator
I want to create and manage doctors
I am signed in as an administrator
Scenario: Doctors List
Given I have doctors named Ram,Shyam
When I go to the list of doctors
Then I should see "Ram"
And I should see "Shyam"
Scenario: Create Doctor
Given I am on the list of doctors
When I follow "Create New"
Then I fill in "Full Name" with "Bishu"
Then I fill in "Address" with "Kathmandu,Nepal"
When I press "Create New"
Then I should see "Doctor was successfully created."
And I should see "Bishu"
And I should see "Kathmandu,Nepal"
Scenario: Edit Doctor
Given I am on the list of doctors
Given the following doctor records
|full_name |address |
|Shyam |Butwal,Nepal |
When I am on Edit "Ram"
And I fill in "Address" with "Birgunj,Nepal"
When I press "Save"
Then I should see "Doctor was successfully updated"
And I should see "Birgunj,Nepal"
“Feature” tag gives general information of the feature that we are going to implement.
The general format is:
As a ,
I want ,
So that .
“Background” tag provides base for implementing stories. The background information will be used by all scenarios. Here, the user will be signed in as administrator to run all scenarios.
“Scenario” is actual behavior that we want to be implemented in our system. The general standard is
Given “The condition that is available for the certain behavior to work on.”
When “The steps that need to be performed to get the behavior.”
Then “The behavior that needs to be fulfilled after the steps is followed.”
In addition to these three tags, “And” and “But” are also available.
After these features are written, they are run using cucumber command.
We always start writing the feature file before writing any production code. So, running this feature file in cucumber, we get error messages. Then, we write the production code to pass the features and run the feature file again. This is known as red/green/refactor cycle of BDD.
The main advantage of BDD is that the features are written in plain natural language. We start writing code to implement those features. The changes in the requirement is made in the feature file and we again write code to pass cucumber feature file. This way, the communication between the clients and developers is smooth. This also covers the area of testing and documentation.
You can get the entire working sample example at https://github.com/anepal/scheduler
- The Rspec Book, Behavior Driven Development with RSpec, Cucumber, and Friends - David Chelimsky et al.
- Why I like Behavior Driven Development - softwaretestpro.com
- Behavior-Driven Development - code-magazine.com
- Agile-Software Development Methodology - T.K. Amudha
- Introducing BDD - Dan North