Skip to content

SpecFlow+ Installation Walkthrough

The "Given, When, Then" syntax of behaviour-driven development (short BDD) is a great way to get the business involved in the software development process. SpecFlow allows you to use this kind of example based specification with your .Net projects. Unfortunately, there are two obstacles that make the installation of SpecFlow and its commercial extension SpecFlow+ a challenge. This post will guide you through the installation process and help you to overcome those obstacles.

When you google for BDD tools, you quickly end up with Cucumber. SpecFlow is the official implementation for .NET. It is open source and provided under a BSD license. SpecFlow+ extends SpecFlow with a reporting function and an integration into the test runner of Visual Studio. Those features require a license that must be installed on your computer. If you don’t have the license the reports will be marked as unlicensed and the test execution has a random delay that will annoy you quickly.

Installing the SpecFlow Extension

SpecFlow offers a nice integration into Visual Studio. You should therefore start with installing the SpecFlow extension into your Visual Studio. Open the tools menu and select “Extensions and Updates…”. In the extensions dialog, select online as the source and search for “SpecFlow”:

SpecFlow has an extension for Visual Studio

Install this extension and restart Visual Studio.

Using SpecFlow+ with your Project

Create a new project (for example a class library project for the .Net Framework) and open the NuGet Package Manger:

go to Manage NuGet Packages in the Solution explorer

Search for SpecFlow in the “Browse” tab and install those 3 packages:

Install SpecFlow, SpecRun.SpecFlow and SpecFlow.NUnit

(If you don’t want SpecFlow+ you need only the first and the last package)

You can now add a feature file over the “Add -> New Item” dialog. The SpecFlow extension offers you feature file templates for various languages. Those files contain a simple example that you can remove and write your own examples. But for the next step you should keep it a bit.

Fix the may only appear once error

If you created the feature file and try to compile your project, you end up with this error:

The custom generator for your feature file doesn’t know what to do. Unfortunately, the message doesn’t help much, then it isn’t explaining where that element should not exist multiple times.

Open your app.config file to find an explanation on how you can fix this problem:

The configuration file has an entry for SpecRun

The solution is right in front of you, but you can’t see it to the lack of formatting. If you remove the last > in the configuration file and add it again, then the editor will format the XML correctly:

Change the entry to SpecRun+Nunit instead of the two unitTestProvider entries you get from the packages

You now can clearly see two entries of and an explanation how you can use SpecRun (the test executor of SpecFlow+) and NUnit. Remove the second entry for and copy the SpecRun+Nunit into the name of the remaining one.

If you compile your project the error will be gone. If it persists, clean your solution, compile again. If that’s not enough, restart Visual Studio.

Fix Default.srprofile XML error

Attention: This problem is fixed with SpecFlow+ Runner 1.6.2. If you can update to the newest version, otherwise this fix can help you.

When you can compile your solution, you can generate the step definitions for your feature file. If you try to run your scenario with the TestRunner it compiles your project and then fails with this error:

Default.srprofile:There is an error in XML document (3, 28). -> Expected hex 0x in '{0}'.

This is a known issue with Visual Studio 2017. The file Default.srprofile isn’t fully generated. If you open it you see that there is a settings-tag in line 3:

<Settings projectName="" projectId="{}" />

Add a project name (for example the name of the project where this file is located) and enter a GUID between the {} of projectId. You can copy the GUID from the *.csproj file or create a new one. When you are finished the line should look like this example:

<Settings projectName="Demo.Spec" 
projectId="{b4873e07-ef9b-4245-80eb-c1afbe580e67}" />

Save the file and start your test again. This time it should execute your specification and report that there are pending steps. If this is the case, you completed the installation and can start writing your own examples.

Conclusion

A formatting issue and a bug in NuGet makes the installation of SpecFlow+ a challenge. They aren’t big obstacles, but when you don’t know your way around you may spend hours trying to get it work. If those two issues are dealt with you can write your BDD specifications without further ado.