From the first Specflow tutorial C#, Bdd with Specflow , I believe you now have a good background on how Specflow fits into Test Automation. Now you understand that it is primarily a tool to aid Behavioral Driven Development (BDD). I will now go into setting up a Specflow project in visual studio.
- Tools installation in Visual Studio
- First Feature file
- Binding Step Definition
- Step Definition Implementation
Pre-requisite and Tools:
- Visual Studio 2015 or higher
Tools Installation in Visual Studio
To start with, spin up visual studio and create a Solution with a unit test project. We will name the Solution TatAuto and the Project TatAuto.Tests
- In the Solution, create Two folders named: Features and Steps
- Delete the UnitTest1.cs file that was automatically generated.
You should end up with a solution that looks like this:
The first tool we are going to install is the Specflow extensions to integrate with visual studio. It will add feature file templates, assist in step definition generation, color assist etc.
So in Visual Studio;
- Select Tools -> Extensions and updates from Visual studio IDE
- Select Online from the left
- On the Right hand pane Search for specflow
- Install Specflow for Visual Studio 2015 or 2017 depending on your version of Visual Studio.
You may need to restart your IDE after installation.
The Next tool is the actual Specflow library for executing Specflow tests. This is available as a NuGet package.
- So right click on the Project and select 'Manage NuGet packages'.
- Search for Specflow.MsTest.
Note that apart from Installing Specflow, this has also added an entry into your App.config file.
<unitTestProvider name="MsTest" />
The unitTestProvider line is where you specify the unit testing framework you plan to use. In our case, it is MsTest. Specflow supports other unit test frameworks out of the box including NUnit.
First Feature File
We are all setup for writing Specflow tests! Now let us add the first feature file into our project.
- Under the Feature folder in the Test Project, Right click and add new item
- Under C# Items you should see the feature file Templates
- Select one in your preferred language. I will select the general one in English
- Rename as Calculator.feature and click OK.
- A sample Requirement and scenario with structure similar to what we discussed earlier in the previous post , will be included in the file generated.
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers
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
- Do a Build of your Solution.
- Go to Test Explorer, you should see the tests displayed.
(You can bring up your Test Explorer window by going to Test -> Window -> Test Explorer )
- Right Click on the Test and select 'Run Selected Tests'
This Should fail as expected. This is because the feature file on its own is unable to do anything. It will require an underlying code called the step definition to bind to. It is this step code written in C# that will carry out what is specified in the steps.
Binding Step Definition
To generate the Binding,
- Right click anywhere in the feature file or within the Scenario texts
- Select 'Generate Step Definitions' within the feature file.
We will leave everything as default.
- Select generate from the popup dialogue.
- Navigate into the Steps folder we created and save the generated class in there.
- You should now have your binding class automatically created.
Another thing to notice is that the colour of your steps have changed. This will depend on the colour scheme you are using in visual studio. With my colour scheme, unbound steps are in purple and bound steps with step definition defined, are in black.
You can open up the associated binding steps by right clicking your step and selecting 'Go To Step Definition' or you can open the class directly from solution explorer.
The class should look as shown.
Step Definition Implementation
Modify the steps to include the implementation of the calculator. Modify your class as show. it is a simple class to calculate addition of numbers.
Do a build of your solution,
Run the test from test explorer and you should Get a pass.
Click on the Output Link below the Test.
You Should see detailed information on the Test Executed.
Next Post I will go deeper into description of the step bindings. We will create and execute more custom scenarios.
Click Here to Download the Complete Working Solution.
Any thoughts, questions, comments, addition, or anything you don’t like, do not hesitate to leave a comment or contact me. Thank you!