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.

1

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.

2

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

3

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.

4

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.

5

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.

6

78

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