Monday, 3 October 2011

SpecFlow BDD

You can add SpecFlow to your project using Nuget or you can download from: 

After using StoryQ in my previous post I thought I’d show the steps to produce the equivalent tests with SpecFlow.

Once you’ve installed SpecFlow to your machine and added the NUnit.Framework and SpecFlow dlls to your project you can create a SpecFlow Feature file:


The tests won’t pass yet because you need to bind the Step definitions to C# code:


Create a new C# class file and copy and paste the binding code above into it:

We now get running tests that have yet to be implemented:


So we can now fill in the implementation for each of the steps, adding in our BeforeScenario (setup) and AfterScenario (teardown) methods:


To download the source:


  1. Nice post Steven! I especially like that you showed off the execution report.

    Two small comments;
    - you can have SpecFlow generate the step skeleton for you by hitting F12 (Go to definition). If no step definition exists the skeleton will be created and if you want to have to put on your clipboard.
    - I would recommend against writing so much of the implementation details in your specifications. The main benefit of writing in Gherkin is that people who are non-technical can read the specification and understand what is going on. So focus instead on the behavior of the system and not the implementation. The WHAT and not the HOW.
    That's what the step definitions are for - the implementation details.

    That's just my two cents. Liked the post though!