BDD with Specflow

Let us discuss how we can implement BDD tests we have written with Speckflow tool in C#. If you are familiar with visual studio, you can install specflow with following.
In vs 2015 nuget comes by default and following is the command to install the specflow

Install-Package SpecFlow -Version 2.3.1

Once you install this you can use specflow with either nunit tests or MS unit test framework.
If you wish to use MS Unit Test framework, lets create unit test project.


Let us first see how we can implement the methods with Specflow tool we just installed.

Once we get the project structure in the explorer, let’s just add a speflow feature file with following.

Right click on project from project explorer (invoke project explorer if you are not seeing it from view menu). In the Add menu select new item and select specflow feature file.


You can directly type the scenarios that you have written in the feature file from my earlier discussion.

Intro to ShiftLeft and BDD in Automation Testing Context


The advantage of specflow is that it can generate the steps from what we have written in the feature file. Right click on the feature file editable area and select generate step definitions.


Once you select the generate step definitions, it will generate step file with all the definitions like below as raw file. Initially it shows “ScenarioContext.Current.Pending()” indicates that this must be replaced with the definitions of the steps.


Specflow Provides the [BeforeScenario] and [AfterScenario] in the context of scenario. We can implement the methods corresponds to it.

Lets assume we have UITest class where we have written all methods corresponding to Calculator.



Since we have written mock methods in the speflow lets look at how we can implement these steps in the UITest Class.

Code Repository Can be found at SpecFlow Example


Intro to ShiftLeft and BDD in Automation Testing Context

In recent times the shift left is happening trend in the software DevOps.

DevOps is basically a ladder between development and testing which enables continues integration and continues development.

In the context of DevOps shift left testing is getting more focus as the strategy of shift left is to test early and deploy early. (shifting testing left side of the “requirements to development to production roll out” pipe line).

That means the automation testing should happen as and when build is getting generated. The cycle times of test execution must be low so that build can be generated quickly. To do this seamlessly with conventional automated testing tools is a challenge as these tools typically have different UI and not integrated with development environment.

So the ideal solution is to have IDE(Integrated Development Environment) or a plugin for development environments. Be it Microsoft Visual Studio, be it Java Eclipse or be it any other development environment, Test Automation can be performed right from IDE right from the same development environment integrating with favorite unit testing frameworks.

This will be one shop stop for both development and testing.

Selenium is already being great tool for Test Automation right from integrated build environments for C# and Java for Web Applications.

So where will BDD fit, BDD is approach for faster testing and provides transparency in testing “what you write as requirement (feature) is what you test”. Requirements are written as features and features are implemented and tested.

It starts with Features, which all features we want to include. And each features contain multiple scenarios and each scenario can have flavors with different data sets.

Application -> Features – > Secenarios -> DataSets

Let us try to write the feature and scenarios for Calculator application in general to get to better understand, and after that we can see how these features can implemented.

Lets first create feature file as calc.feature  and write following.



Then lets choose how many types of scenarios we want to cover for this feature and tag them separately.



Let us take in each of these operations lets write basic scenarios with keywords Given, When and Then.

Given describes or sets the context of the scenario under stated environment. The best practice is to check the system state. Its like prerequisite for the given scenario.

When describes the state change parameters. Like user actions to move the system from one state to another state.

Then describes the consequence or result caused by the state change. Its like if when is the cause Then is the effect.

And is key word will be used to add multiple whens or multiple Thens.

But is used in the same context as then and mostly used for negative testing.

Here is how a scenario looks like.scenario example

The tag @Basicoperations is like category of the test.

Scenario Outline can contain multiple examples, It is always better to write as Scenario Outline instead of just scenario so that we can specifically mention the parameters and looping of the test data is also possible.

We have several ways of writing the same scenario. Its better to write the scenario as re-usable blocks so that the steps can be re-used.

Let us take another example to see how we can re-use the same steps.

In the below scenarios same steps are used as above scenario and just the operation is being changed.


Let us look at @Advancedoperation description and figure out how we can write the detailed re-usable steps for more advanced operations.


In the above we can see multiple and statements and some of the “And” statements are similar. The above scenario enters data in below form. the multiple And statements enter the values in the respective text boxes.


This is very small example of how we can write BDD tests, I will try to explain how this can be implemented in next blog.