BDD with SpecFlow Tutorial
What is Behavior driven development (BDD)¶
In software engineering, Behavior Driven Development is an agile software development process that encourages collaboration among developers, QA and non-technical or business participants in a software project.
So in a nutshell, BDD helps bridge the communication gap between the development team and relevant stakeholders.
There are several tools depending on the software platform that are used in the BDD process. For instance Java has Cucumber and in C# we have Specflow.
What is SpecFlow¶
SpecFlow is an open source testing framework supporting BDD practices in .NET development platform. SpecFlow helps you define the behavior of your application in plain English (other languages like French are also supported) using Gherkin.
Why BDD with Specflow¶
- Specflow works feature files that contain the description of the feature being tested, the scenarios surrounding the feature and the steps involved in the scenario.
- Feature files are made of requirements that have been agreed upon between developers, QA and non-technical team. This means dev teams are more likely to deliver apps that meet all the requirements.
- The feature files also serve as documentation and is version controlled just like the source code.
Gherkin¶
Gherkin is a domain specific language for defining tests that is readable. It uses plain language to describe use cases and allow users to remove logic details from behavior tests. This link Gherkin Editor by SpecFlow enables you to quickly write and share feature files and Scenarios.
Here is Sample Feature File with Gherkin Syntax:
Feature: Calculator
Simple calculator for adding two numbers
@mytag
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen
@mytag
Scenario Outline: Add two numbers
Given I have entered <First in the calculator
And I have entered <Second into the calculator
When I press add
Then the result should be <Result on the screen
Examples:
| First | Second | Result |
| 50 | 70 | 120 |
| 30 | 40 | 70 |
| 60 | 30 | 90 |
Feature File¶
The above example of Gherkin syntax shows an example of a SpecFlow Feature file. The extension for the file is .feature
. The name of the file usually describes what feature is being tested. It has a header with description that starts with Feature:
followed by the name of the feature.
Scenarios start with Keyword Scenario:
followed by a description of the scenario
Steps start with Gherkin keywords like Given, When, Then, And, But etc.
- Given - For Setting up the state of the system for testing, usually a known state
- When - To Perform an action on the system
- Then - Assertion step for the Expected Outcome.