Configuring SpecFlow and NUnit in Visual Studio 2015

How to set up Specflow and NUnit v3 in Visual Studio 2015. I have already added a new Class Library project named SpecFlowTest to an empty solution.


Step 1. Install SpecFlow extension for Visual Studio 2015

Once the SpecFlow extension is installed you will be able to add SpecFlow items to a project from the Installed Templates list of the Add Item dialog box.

  1. In the Tools menu select Extension and Updates…. This will open the Extensions Manager dialog.
  2. In the dialog choose the Online gallery in the left pane.
  3. In the right pane type specflow in the search box.
  4. In the middle pane find Specflow for Visual Studio 2015 in the results list and click the Download button. If you already have it installed a installed-tick tick will be displayed instead of a download button.

specflow-extension-vs2015

The package will be downloaded

specflow-extension-vs2015-download

A dialog will appear with a summary on the installation and prompting confirmation of the install

specflow-extension-vs2015-install

The installation will complete.

specflow-extension-vs2015-installing

Once installed a web page normally opens in Visual Studio with this address, http://www.specflow.org/guidance/first-steps. In some cases a restart of Visual Studio may be required.


Step 2. Installing NuGet packages

As usual NuGet packages can be installed via the window interface
Tools > NuGet Package Manager > Manage NuGet Packages for Solution…
or if you prefer, via the command line
Tools > NuGet Package Manager > Package Manager Console
There are a few ways to add the SpecFlow and NUnit libraries to a project,  either by installing individual packages for each product or by using combined packages.

Option 1. Combined Package

Open NuGet Package Manager and locate the SpecFlow.NUnit package.  This option will install both the SpecFlow and NUnit libraries as well as the NUnit runners for running the tests from TestExplorer in Visual Studio.  In the package description it does say NUnit version 2.6+ will be installed, at the time of this post NUnit version 3.0.0 was installed. In the future this may be a newer version of NUnit.
nuget-vs2015-specflow-nunit
And to install via command…

Install-Package SpecFlow.NUnit

Although NUnit version 3.0.0 is installed it can be upgraded to the latest version.  At this time the current version is 3.4.1.

nuget-vs2015-nunit3-update

And the NUnit update via command
Update-Package NUnit

And with that everything should be good to go.


Option 2. Installing Individual Packages

The packages can be installed separately.  Doing so would allow you to install a different test runner or a different unit testing framework.  Whatever the reason, if that is the case then you can follow these steps.

Install SpecFlow
With NuGet Package Manager open search online for the SpecFlow package.

nuget-vs2015-specflow

And via command

Install-Package SpecFlow

Install NUnit

This will install version is 3.4.1, no need for update.
nuget-vs2015-nunit3-install

And via command

Install-Package NUnit

Install NUnit3TestAdapter

Here I am installing a different test runner to the one installed in the combined package.  The result is the same, tests can be run from within Test Explorer.
nuget-vs2015-nunit3-testadapter

Install-Package NUnit3TestAdapter

If feature files have already been added to the project the below message will prompt for them to be re-generated.

specflow-change-detected

Conclusion

It seems logical to use the SpecFlow.NUnit combined package if NUnit is the unit test framework of choice.  If you will be using MSTest or a different test runner installing separate packages is not that much more work.

Setting up a Visual Studio project for testing with NUnit and SpecFlow

In this article I am going to run through my currently preferred set up for Specflow and NUnit. I have already added a new Class Library project named DemoProject.Tests to an empty solution.


Step 1. Install SpecFlow extension for Visual Studio

Once the SpecFlow extension is installed you will be able to add SpecFlow items to a project from the Installed Templates list of the Add Item dialog box.

  1. In the Tools menu select Extension and Updates…. This will open the Extensions Manager dialog.
  2. In the dialog choose the Online gallery in the left pane.
  3. In the right pane type specflow in the search box.
  4. In the middle pane find Specflow for Visual Studio 2013 in the results list and click the Download button. If you already have it installed a tick will be displayed instead of a download button.

specflow-visualstudio-extension
The extension will be downloaded.
specflow-download
Click Install to complete the installation.
specflow-install

You will be prompted to restart visual studio.


Step 2. Install NUnitTestAdapter

This step is optional.  I like to run NUnit tests from within the Test Explorer in Visual Studio rather than running the tests using the NUnit external application.  At this moment NUnitTestAdapter is not compatible with NUnit 3.0 so in the following steps I will ensure NUnit 2.6.* is installed.

Open Package Manager Console and run the following command.

Install-Package NunitTestAdapter -version 2.0.0 -projectname DemoProject.Tests

Step 3. Install the Specflow

Open Package Manager Console and run the following command.

Install-Package SpecFlow -ProjectName DemoProject.Tests

The relevant SpecFlow assemblies will now installed in the specified project.


Step 4. Install Specflow.NUnit

If you run this command without the specific version parameter this will install Specflow.NUnit 2.0.0 which includes NUnit 3.0.0.  As I intend using NUnitTestAdapter (currently only compatible with NUnit 2.6.4) I do not want NUnit version 3 installed so I have specified the -version 1.1.1 parameter, this will install Specflow.NUnit 1.1.1 which includes NUnit 2.6.0.

Open Package Manager Console and run the following command.

Install-Package SpecFlow.NUnit -version 1.1.1 -ProjectName DemoProject.Tests

Step 5. Update NUnit Version
Open Package Manager Console and run the following command.

Install-Package NUnit -version 2.6.4 -ProjectName DemoProject.Tests

This will have updated the NUnit version from 2.6.0 to 2.6.4.


For additional information see the following links:
http://www.specflow.org/resources/
http://www.specflow.org/getting-started/
http://www.specflow.org/documentation/Install-IDE-Integration/
http://ralucasuditu-softwaretesting.blogspot.co.uk/2015/06/write-your-first-test-with-specflow-and.html