Saturday, March 3, 2012

SpecFlow 101

SpecFlow is a Behavior Driven Development (BDD) tool for .NET.  SpecFlow has brought the Gherkin language to .NET and to Visual Studio!  This post will set you up quickly with SpecFlow with a simple example.  


Download the latest SpecFlow install

New Visual Studio Templates


Once installed, there are three new Visual Studio templates that will be installed including the

  • Event Definition – this template is to be used for more of global hooks such as
  • Feature – template to write out the feature with scenarios in Gherkin language
  • Steps Definition – template to store the steps from the feature file that will execute the code and do assertions

Our Feature Example

For this example, we’ll use the Roman Numeral kata.  While this is a relatively simple kata, the purpose is to help navigate through SpecFlow.  So, let's begin:

1.  Create a new project and create a SpecFlow Feature File:


2.  Create a SpecFlow Step Definition File

The Step Definition template has code with sample steps in the class.  Delete everything inside the class but not the class itself.

3.  Generating Steps from Feature file

There are a couple of different ways to generate steps once a scenario is given.

  • Step by Step – with your cursor over a scenario step, press F12.  image

SpecFlow will offer to put the step binding skeleton in your clipboard and then it can be easily pasted into your steps definition file.  Note that if the step already exists, Specflow will navigate you to the step definition automatically, just like F12 does with “GoTo Definition”.

This is useful when you’re creating a new feature and want to take one step at a time versus having numerous pending steps.  I find this useful when you want to take things one at a time.

  • Bulk Create

Using TestDriven.Net right click on the feature designer file (extension of .feature.cs) click “Run Test(s)”.  For each scenario, SpecFlow will create a task that contains a StepsDefinition class.  This class can be copied and pasted into the steps definition file.   It should look like the following:



  • With each generated step, the result will be Pending
  • One class per scenario.  When generating steps, SpecFlow will generate one steps definition class that is in the feature file if none exists.  There could exist the same step in multiple scenarios, neither of which have been generated yet.  When pasting in the generated steps, you’ll get compile errors telling you of duplicate steps.  Delete the duplicate steps and you’re golden.
  • If two of the same steps exists and you manually change the method name but leave the regex decorator the same, there will be a runtime error noting ambiguous steps.
  • If attempting to navigate to a step from a feature file with multiple regex step definitions matching, SpecFlow will throw a dialog box up stating ambiguous steps and will navigate to the first one.

Developing Code to Make The Test Pass

By default, SpecFlow is setup to use NUnit.  You can configure SpecFlow to use MSTest, if so desired.  Generate the steps with the correct xUnit assertion statements to make the tests pass for each step. 

Running Tests

Running Tests are very similar to generating the steps.  Once the steps are generated and beautiful code is written to get the tests to pass, right click on the feature designer file and click “Run Test(s)”.  If steps within a scenario are still pending, the following message will be displayed:


Hopefully this gets you familiar with SpecFlow and some of the advantages of this tool being integrated into Visual Studio.  Happy trails with your SpecFlow journey!