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”:

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:

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

(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 <unitTestProvider> 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 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:

You now can clearly see two entries of <unitTestProvider> and an explanation how you can use SpecRun (the test executor of SpecFlow+) and NUnit. Remove the second entry for <unitTestProvider> 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:

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:

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.

4 thoughts on “SpecFlow+ Installation Walkthrough”

  1. A couple of quick points:
    Depending on whether you are using SpecFlow or SpecFlow+, the packages you want to install will be different.
    – If you are using SpecFlow, and want to use NUnit, install the SpecFlow and SpecFlow.NUnit packages.
    – If you are using SpecFlow+ Runner, only install the SpecRun.SpecFlow package.

    So, when only using SpecFlow (not SpecFlow+), you will want to install the SpecFlow.NUnit package (or an alternative to NUnit) as well as SpecFlow.

    Installing the SpecRun.SpecFlow package will automatically install SpecFlow and the other required packages. SpecFlow.NUnit is not used by SpecFlow+ Runner, and is the reason you end up with 2 unitTestProvider entries in the app.config. If you don’t install SpecFlow.NUnit, you avoid this, and only need to change the entry to “SpecRun+NUnit” (or whatever you want). You don’t NEED to use NUnit of course with SpecFlow+ Runner.

    You can install your favourite assertion framework (e.g. NUnit) to use together with SpecFlow+ Runner. But in this case, it is better to simply install the NUnit package directly, rather than SpecFlow.NUnit. You can also use XUnit, MSTest, FluentAssertations…

    Reply
    • Hi Stephen,
      Thanks for your explanation. So far I got strange errors if I didn’t use SpecFlow.NUnit with SpecFlow+. Strange as in Assert.AreEquals() throws a NullPointerException – not for the values I compare but for Assert. I will give it another try.

      Thanks,
      Johnny

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.