Installing SpecFlow in Visual Studio 2013 Premium and up

In this post we go through the steps needed to get SpecFlow up-and-running in Visual Studio 2013. I’ve done this using the Premium edition and it should work on most editions except for Express.

Install SpecFlow

Click on Tools -> Extensions and updates:

Extensions and updates

Click on ‘Online’ and type in SpecFlow in the searchbox:

Extensions and updates

Click on the ‘ Download’ button and Visual Studio will download SpecFlow:

Download of the extension

When the download is complete, Visual Studio will show the following window:

SpecFlow's license agreement

Just click on the ‘Install’ button. Once Visual Studio has completed the install, it will show the following window. Click on ‘Restart now’ to restart Visual Studio:

Restarting Visual Studio after SpecFlow installation

Create a new Test project in Visual Studio

From Visual Studio’s menu click on File->New->Project. Then select the ‘Test’ template and choose ‘Unit Test Project’. Enter the name and location of the project conform your usual habits.

Creating a Test project

You now have a Solution that contains a Unit Test project which in-turn contains one empty test case.

Empty testcase

Setup your project to use SpecFlow

In earlier steps we installed the SpecFlow add-on. However, each project that wants to use this add-on, needs to set its references correctly. The NuGet package manager can do this for you. Use the following command in the Package Manager console to achieve this:
Install-Package SpecFlow -ProjectName SpecFlowAndTestRailIntegration
Using NuGet to setup your project's references

This has added an App.config  to our project:

Your App.config has been updated

And it has setup the references for us:

References have been updated

Add a SpecFlow testcase to the project

SpecFlow testcases, also known as ‘scenarios’ are contained within a feature file. One file can contain many testcases.

Right click on your project in the Solution Explorer, choose ‘Add->New Item’. This will show the following dialog, where you can see three types of SpecFlow related files. In this case, we will add a ‘ SpecFlow Feature File’

Adding a new .feature file

Click on ‘Add’  and the feature file will be added to the project:

.feature file has been added

Delete ‘UnitTest1.cs’

This file was generated by Visual Studio when we created the project, we don’t need it, so just delete ‘UnitTest1.cs’

Bindings: Getting the testcases to actually do something

When you add a new .feature file, SpecFlow fills it with a small default testcase:

SpecFlow's default content

In the above example, you can see that some lines contain purple text. This is SpecFlow’s way of indicating that these steps in the testcase don’t do anything yet and that you need to create some code that actually does what the step needs. This code is known as a ‘Binding’. Right click on the following line:

Given I have entered 50 into the calculator

and a context menu will appear:

Popup when right clicking in a .feature file

Select ‘Generate Step Definitions’

Generating step definitions

In this dialog window, SpecFlow allows us to control for which steps we want to create bindings and what the name of the C# class and file will be. For sake of this example, just accept the defaults and press ‘Generate’.

Saving the generated stepdefinitions

Press ‘Save’ and we now have a new .cs file in our project:

Project contains a new .cs file

This file contains the following not-so-useful code. We will complete this code later on.

using System;
using TechTalk.SpecFlow;

namespace SpecFlowAndTestRailIntegration
{
    [Binding]
    public class SpecFlowFeature1Steps
    {
        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int p0)
        {
            ScenarioContext.Current.Pending();
        }

        [When(@"I press add")]
        public void WhenIPressAdd()
        {
            ScenarioContext.Current.Pending();
        }

        [Then(@"the result should be (.*) on the screen")]
        public void ThenTheResultShouldBeOnTheScreen(int p0)
        {
            ScenarioContext.Current.Pending();
        }
    }
}

Configuring which unittest framework to use

If you compile the solution at this point in time, then you would probably get a bunch of the following compilation errors:
The type or namespace name 'NUnit' could not be found (are you missing a using directive or an assembly reference?)
This is due to the fact that the default App.config for SpecFlow, wants to use the NUnit framework which we don’t have in this example. Instead, modify the App.Config to use Microsoft’s test framework as shown below:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
  </configSections>
  <specFlow>
    <unitTestProvider name="MsTest" />
    <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
  </specFlow>
</configuration>

When we save changes to the App.config, SpecFlow detects this and will ask you the following question:

SpecFlow asking to regenerate codebehinds

It’s almost always fine to just press ‘Yes’. Now when we build the solution, we will only get 3 informational messages that can be ignored:

Three remaining messages

Finishing the code in Binding

To actually get the teststeps to do something useful, we need to implement the different steps in the binding. Replace the content of the .cs file with the following:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using TechTalk.SpecFlow;

namespace SpecFlowAndTestRailIntegration
{
    [Binding]
    public class SpecFlowFeature1Steps
    {
        private List<int> inputs;
        private int result;

        public SpecFlowFeature1Steps()
        {
            this.inputs = new List<int>();
        }

        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int p0)
        {
            this.inputs.Add(p0);
        }

        [When(@"I press add")]
        public void WhenIPressAdd()
        {
            this.result = 0;
            foreach(int number in this.inputs)
            {
                result += number;
            }
        }

        [Then(@"the result should be (.*) on the screen")]
        public void ThenTheResultShouldBeOnTheScreen(int p0)
        {
            if(p0 != this.result)
            {
                Assert.Fail(String.Format("Actual result {0} does not match expected result {1}",
                    this.result, 
                    p0));
            }
        }
    }
} 

Run the test

Build the solution, this should now build without any errors and open the Test Explorer window (Test->Windows->Test Explorer) and click on ‘Run All’

Testcase has run successfully

Congratulations, you’ve just run a SpecFlow test!