C# ATDD on a shoestring (or the complete guide to SpecFlow & NUnit in Visual Studio 2010 Express)


When I’m working on something new at work, I like to be able to work on it in my own time at nights and on the weekend to polish my skills through experimentation, and share what I learn. I’ve recently been working on setting up Acceptance Test Driven Development in C# at work, but unfortunately Microsoft makes it really, really hard to polish your skills in C# development at home, unless you’re willing to part with large sums of cash (say from $20,775, and I’d prefer a new car). This is probably one of the reasons why I find the Microsoft .NET testing community (and the dev community too) quite insular, Microsoft just doesn’t encourage their users to share their craft.

Sure, Microsoft provides Visual Studio 2010 (VS2010) Express Edition – which can be used free of charge, even for commercial purposes, but the limitations on its functionality are borderline ridiculous. For example, it doesn’t support Microsoft’s own unit testing framework (Microsoft obviously don’t encourage TDD), and all add-ins are banned (no ReSharper for you). It also doesn’t work with Microsoft’s own version control system (TFS).

So, Microsoft it basically makes it really hard to do anything you want to do, but there are ways to get stuff done, even if they’re convoluted.

ATDD Frameworks in C#

I know of three main open source ATDD/BDD frameworks that work in C#: SpecFlow, Cuke4Nuke and StoryQ. Only two of these use Gherkin (StoryQ uses its own slightly modified DSL), and I like Gherkin, so I’ll ignore StoryQ for now. Cuke4Nuke still requires some Ruby glue, and to keep things pure, I like SpecFlow as it’s a pure .NET implementation of ATDD with full Gherkin syntax support. For this reason, SpecFlow seems to be the most prevalent ATDD framework in the .NET community and this is what I will use for this example.

SpecFlow as an Acceptance Test Driven Development Framework

SpecFlow is an open source framework that is delivered as a Windows installer, and this installs support for feature and step definition files in Visual Studio through an Add-In that automatically generates unit tests for you from the feature files. Unfortunately, as VS2010 Express doesn’t allow add-ins, SpecFlow won’t work without some tinkering.

Install SpecFlow & NUnit

Before we get started, we need to install VS2010 Express, SpecFlow and NUnit.

  1. Install VS2010 C# Express if you don’t already have it
  2. Install the latest SpecFlow version from the SpecFlow GitHub downloads page.
  3. Install the latest NUnit version from the NUnit download page.
  4. Add the Specflow directory (C:\Program Files\TechTalk\SpecFlow or similar) and the NUnit directory (C:\Program Files\NUnit 2.5.9\bin\net-2.0 or similar) to your Windows path.

Adding SpecFlow templates to Visual Studio 2010 Express

When installing SpecFlow to VS2010 (non-express) it creates templates that can be accessed through the “New Item” menu. Fortunately, it is fairly trivial to add these to VS2010 Express. Just download the templates I have uploaded here (2010, or here (2012) unzip this directory and place the .zip files (SpecFlowFeature.zip, SpecFlowStepDefinition.zip etc.) into your Visual Studio 2010 Express C# templates directory (usually: C:\Users\username\Documents\Visual Studio 2010\Templates\ItemTemplates\Visual C# or similar).

This means you now have access to the SpecFlow templates when adding a new item to your C# project.

Generating SpecFlow NUnit tests from Feature Files

When you add a SpecFlow feature to the full version of VS2010, it automatically generates a unit test file behind the feature file, so that it’s always up to date. This doesn’t work in Express edition, so you have to use the specflow.exe file to do this for you. Fortunately, this can be done in a batch file, and then added as an “external tool” to VS2010 Express, so you get the same functionality, but not quite as neat.

  1. Enable Expert Settings in VS2010 Express (if not already enabled), by choosing Tools->Settings->Expert Settings.
  2. Create a batch file in your project directory called something like “generatespecflowtests.bat” with the following content:

echo Starting to generate SpecFlow unit tests
SpecFlow generateAll %1 /force /verbose
echo Finished generating SpecFlow unit tests
  1. Add an external tool command in VS2010 Express, using Tools->External Tools->Add. Make sure you set the starting directory, and “show output”

  1. You can then generate your tests from the Tools menu or, if you like, you can add this command to a new Toolbar by using Tools -> Customize -> Toolbars -> New, then Tools -> Customize -> Commands -> Toolbar -> Add Command.

Running SpecFlow NUnit tests from Feature Files

Simon Whittemore from London wrote an excellent post about how to automatically run Specflow NUnit tests, and capture a pretty html result, on his blog. I’ve included his batch file here, as it’s what we’ll use to run our tests from within VS2010 Express.

  1. Create a batch file in your project directory named runspecflowtests.bat or similar.
  2. Include Simon’s content (below) in your batch file.
@echo off
nunit-console %1
specflow.exe nunitexecutionreport %2 /xmlTestResult:%3
if NOT %errorlevel% == 0 (
echo "Error generating report - %errorlevel%"
GOTO :exit
if %errorlevel% ==0 TestResult.html
  1. Create an external tool setting the same as we did for the generate unit tests, but with the parameters defined on this dialog:
    1. Command: Link to your batch file
    2. Arguments: $(TargetName)$(TargetExt)$(ProjectDir)$(ProjectFileName)$(BinDir)TestResult.xml
    3. Initial Directory: $(BinDir)
    4. Use Output Window: checked

  1. Optionally add this to your VS2010 Express Toolbar as above

When you run this command in VS2010, all SpecFlow features are run, and the resulting report is launched in your default web browser.


What I have shown is that although Microsoft makes it very hard, it is possible to use VS2010 Express and open source tools like SpecFlow so that you can polish your skills in your own time.

In this theme of ATDD using C# and .NET, over the coming days I will demonstrate how you can use SpecFlow to write and run automated web tests against a web browser using the open source tool WebDriver. Stay tuned!

Update: 20 Feb 2011

The original link to the templates was wrong, I have now updated it to the correct link.

Update 29 Sep 2013

Yannick de Kercadio has kindly provided VS2012 templates here.


Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.

36 thoughts on “C# ATDD on a shoestring (or the complete guide to SpecFlow & NUnit in Visual Studio 2010 Express)”

  1. Excellent post! I’ve seen fitnesse, but it didn’t fit in with our MS toolset (VS, Sharepoint, TFS), but this may just do the trick.


  2. Great post, really helpful for those of us who wish to use this type of functionality with Express.

    Had a couple of issues, maybe the following would be of help to others who have had the same problem:

    When generating scripts the class files are put in the root but alteration to the project file tidies this up:


    Could not get the report external tool to work but the following worked as per SpecFlow screencast:

    In NUnit: Tools>Save Results as XML

    In VS: External Tools
    Command: F:\Program Files (x86)\TechTalk\SpecFlow\specflow.exe
    Arguments: nunitexecutionreport $(ProjectFileName)
    Initial Directory: $(ProjectDir)

    Thanks again


  3. Sorry, ‘Test.feature’ above stripped out xml.

    Add the feature file as a DependentUpon attribute of the ‘Compile Include=”yourtest.feature.cs”‘ entry in the project file.


  4. The generatespecflowtest.bat, as above in [1], has hard-coded you “project name” (SpecFlowWebDriver) ?? Shouldn’t it be as in [2] ??

    echo Starting to generate SpecFlow unit tests
    SpecFlow generateAll SpecFlowWebDriver.csproj /force /verbose
    echo Finished generating SpecFlow unit tests

    [2] #using variable $(ProjectDir)$(ProjectFileName).sln as parameter
    echo Starting to generate SpecFlow unit tests
    SpecFlow generateAll $1 /force /verbose
    echo Finished generating SpecFlow unit tests


    1. I have finally updated my example above to use the parameter as you mentioned. You only need to project file name, and use a %1 instead of $1.


  5. For the external tool to run the tests, make sure you put spaces between the three arguments (the text given does not contain the spaces):

    $(TargetName)$(TargetExt) $(ProjectDir)$(ProjectFileName) $(BinDir)TestResult.xml

    I’m running on 64-bit Windows, so I had to force NUnit to run in 32-bit by changing the following line of runspecflowtests.bat:
    nunit-console %1
    nunit-console-x86 %1

    Thanks for the post. It looks promising.


      1. Thanks, Todd. It worked when I added these two spaces. Alister, wondering if you could add those two spaces to your blog text to help future generations. Thanks! -Ben


  6. Hi Alister,

    i want to generate the test result in the html format from whatever i had saved it in .xml file from the Nunit. I followed ur steps but somehow i am not able to generate that thing. it is giving me the nunit-console %1 is not a valid command when i run it. Can you please help me out on this?

    Anand K.


  7. This works really well, thanks! However, it looks like SpecFlow nunitexecutionreport has an issue when a feature uses Scenario Outline (instead of just Scenario). It seems to skip these features in the HTML generation (although they are in the XML). Thoughts or workaround?


      1. Hello, I changed the first entry of the XML which said <test-suite type="ParameterizedTest"
        <test-suite type="TestFixture"

        and now it works repeatedly (even for later features). Go figure. Thanks for the help, your post has helped speed up my testing!


  8. I couldnt download VisualStudio2010ExpressSpecFlow.xml . Can some one provide me the link…


  9. Incredible post! Thanks so much! I have everything setup w/in DevExpress as outlined above. When I run the batch file to generate the tests, it is creating the test cs file based on the feature definition. I see this when I click to show all files, however, the .cs file is not automatically added to the project, therefore it’s not getting built when I try and build the project through VS. Do I need to do something extra to get the auto-generated file included in the project? Auto-build some other way? Are you having this issue? Thanks!


    1. You should be able to use the ‘include file in project’ in visual studio when right clicking the generated file.


      1. Cool, that’s what I was doing, but I wasn’t sure if there was another automated way. I actually modified the template file for the feature to add a feature.cs (blank) to the project so I didn’t have to do this. Unfortunately, there are now two files for each Feature file…gotta love the disabling of 3rd party integration in DEV Express! Thanks again for the post!


  10. when I tried generate template, I only got one file “xxxx.feature”, there is no “xxxx.feature.cs”. Is it right?


  11. Hello,
    Thanks for the nice Post.
    How can we run tests as group(for ex: regression tests) by using above method???
    Many Thanks


  12. Oh Microsoft. Why do you make us work so hard to love you? I’ve taken to re-installing an evaluation copy of visual studio professional whenever I want to do some experimentation work. Another option is to work out of a VM and keep it’s date/time *fixed* to a past point so the evaluation license doesn’t expire.


  13. Thanks Alister for this detailed post! I just wanted to setup my old personal laptop running XP and the VS Express version and this is just what I needed.
    For people who are relatively new to SpecFlow I wanted to clarify a couple things:
    – once you have created a .feature file and run the SpecFlow generateAll command, you will get a code behind for the .feature
    – after that you still have to create/add your Specflow test step class to the project and fill in your custom details – see https://github.com/techtalk/SpecFlow/wiki/Step-Definitions
    – and then finally you can run the NUnit test runner
    Once you’re fully setup this approach works great. Since you can’t debug the steps easily, another tip is to use System.Console.WriteLine profusely in the steps. The output will show up with the rest of the execution.


  14. Thanks for this – it’s been really useful. However I have one issue in that the .feature files don’t have any syntax highlighting in Visual Studio. Is this expected or have I missed something?


Comments are closed.