Now we have a complete understanding of What is TDD, What is BDD and Why & When these are used. We are heading towards the SpecFlow tutorial. In the last chapter we followed the Steps to Install Visual Studio and in this chapter we will learn How to Configure or SetUp SpecFlow.

Steps to Configure or Set Up SpecFlow

SpecFlow tests are defined as “features” that have multiple scenarios. There is a small amount of project setup that needs to be done, but it’s not too bad and you can be up and running in just a few minutes.

  1. Create a New Project
  2. Install SpecFlow Visual Studio integration
  3. Add SpecFlow to the Project Reference
  4. Add NUnit Test Adapter to the Project Reference
  5. Add NUnit Framework to the Project Reference

Step 1 : Create a New Project

You might expect a SpecFlow template to appear somewhere in the project list.  Sorry, that’s not what you get.  You could create one yourself if you really wanted.  To create a SpecFlow project, all you really need to do is to create a Class Library project.

1) Assuming that Visual studio is opened on the machine, Go to File -> New -> Project to create a new Demo Project for the practice.

SpecFlow_1

  1. Select Windows from the left tab, it will display all the windows application project types. Choose Class Library in the center window and name the project accordingly. To keep it simple, please follow the same name give below 'SpecFlowDemo'. Once done, click on the OK button.

SpecFlow_2

  1. A new project will be created with a dummy class 'Class1.cs'. The project will look like the below image. All the project files such as Property Files, References and Class Files  & Folders display under the Solution Explorer at the right side.

SpecFlow_3

Step 2 : Install SpecFlow Visual Studio Integration, NUnit Adapter & NUnit Framework

1) The first thing should be done is to install the SpecFlow Visual Studio integration.  Do this by navigating to Tools –> Extension and Updates..

SpecFlow_4.

  1. Make sure you are in the Online branch on the left and type SpecFlow into the search area in the upper right corner of the dialog. Search will display the SpecFlow Tool at the center, select it and click on Download.

SpecFlow_5

Step 3 : Add SpecFlow to the Project Reference

And now, this is where I always get tripped up for a minute.  You can create a new feature file, but there is a menu item you should see that will allow you to add feature steps to a class file and you won’t see that until you add a reference to SpecFlow.

  1. Now include the SpecFlow tool in the project reference. Navigate to Tools -> NuGet Package Manager -> Manager Nuget Packages for Solutions...

SpecFlow_6

2) Once again a dialog will display. In the search box, type SpecFlow and hit enter. This will display the SpecFlow tool, select the tool and click on Install button at the right side.

SpecFlow_7

A Successfull Message will display in the Console window at the successfull Installation.

SpecFlow_8

  1. The way install NUnit Adapter and NUnit Framework. Still in case face any issue, please refer the chapter

How to write C# Test using NUnit Framework. In this chapter, installation for both tools is given, but still the installation is preety straight forward.

Steps to Configure a SpecFlow Test

Step 1 : Create a Feature File

1) Now that you have the environment setup, you can actually use SpecFlow. To create your first feature file, you can right click on the project and select Add –> New Item... from the context menu.

SpecFlow_9

  1. Select SpecFlow Feature File, give it a logical name and click on Add button. When I name my SpecFlow feature files, I try to name them similar to the feature it will be testing.

SpecFlow_10

3) The feature file will have a sample of how feature will be setup. This is just a sample and the purpose it to display the pattern of the feature file. So, you will be changing these file a lot when you move forward in this tutorial.

SpecFlow_11

Step 2: Generate Step Definition File

In order to test our scenario, the next thing to do is to create a Step Definition file. This is a regular C# file with a Binding attribute added.  The file that SpecFlow will generate will match the template they provided in the feature file. Yes, it can automatically generate a skeleton for the automation code that you can then extend as necessary:

  1. Right-click on your feature file in the code editor and select Generate Step Definitions from the popup menu.

SpecFlow_12

  1. This displays a Pop Up window, which will ask to select the statements for which Step Definition file is to be created. Select all and click on Generate button.

SpecFlow_13

Note: Always choose a sensible name for the files, this is just an example file, I am not bothering about the name, from the next chapter we will follow the rules.

  1. It will ask to specify the folder path to save the Step Definition file, let it be a default project folder and hit the Save button.

SpecFlow_14

  1. All the statements will change the color now, it means these Feature statements are linked with Step Definitions.

SpecFlow_15

  1. To have a look at the attached definitions, click on any statement and press F12 button. This will open up the linked definition file and the cursor will be pointing to the linked definition.

SpecFlow_16

Executing a SpecFlow Test

  1. To identify all the tests in the Feature File, go to Test -> Windows -> Test Explorer.

SpecFlow_17

  1. Notice, at the left side a new window is appeared called Test Explorer. One test is also displayed in the Test Explorer window.

SpecFlow_18

Note: There can be strong chances that the Test Explorer window will not show the default test. Kindly do a build by Build -> Build Solution. if not succeeded, open App.config file from the Solution Explorer window and make sure that unitTestProvider name should contain only one entry, either NUnit or SpecFlow. take help from the above screenshot.

  1. Now to run a Feature Test, Right-click on the test in the Test Explorer window and select Run Selected Tests. This will run the selected test and display the output in the console window.

Note: Feature File can also be run by Right-clicking in the feature and choosing Run SpecFlow Scenarios. But sometimes it creates an issue.

Set Up Selenium WebDriver with Visual Studio in C#
Set Up Selenium WebDriver with Visual Studio in C#
Previous Article
Feature File
Feature File
Next Article
Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M A FULL-STACK TEST AUTOMATION ENGINEER. Have passed 16 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently, I am working with RABO Bank as a Chapter Lead QA. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns.
Reviewers
Virender Singh's Photo
Virender Singh

Similar Articles

Feedback