Getting Started with SpecFlow in ASP.NET

One problem with being an ASP.NET developer is that it seems like the Ruby guys get all the cool toys first. I mean not only do they get all the great presentational goo like HAML and SASS, but they get all the fun testing frameworks too.

Gotta Love the Cucumber

And as testing frameworks go, there is something pretty awesome about Cucumber which is all about tying your specs to your tests.


I love the idea of documenting exactly what I’m going to build and then checking it off a list. There’s a great sense of progress.

For those of you that aren’t familiar with the awesome cucumber framework in the Rails world, the best way is to show you an example.

Step 1: Write A Feature

Here is what a feature definition looks like in cucumber:

CucumberFeaturesCode Photo Credits: Ryan Bates’ Cucumber Screencast

If it doesn’t look like code, that’s because it really isn’t. It’s very much like writing normal language sentences that even a business analyst could understand. The idea is that you write your specs following the Gherkin syntax (note the Given/When/Then) and you tie that to tests in code.

Step 2: Tie it to Code

So we have a spec, then you tie it to code with regular expressions. For example,

Then /^I should see "(.+)"$/ do |text|
  response_body.should =~

Notice how the “text” from the feature gets passed into the testing code. So the same testing code can be reused for multiple features.

Step 3: Code Until Green

Now that your specs are tied to your test code, you simply write your actual code until all of your specs are green (ie – complete).

The nice thing is that you can see exactly where your project is at any moment. Have you completed 20% of the spec? 90% of the spec? Just run your tests to find out.

What about for the ASP.NET Crowd?

This is all good for those hippie Rail programmers, but what about for the more practical enterprise .NET programmer?


Enter SpecFlow. SpecFlow is a clone of Cucumber for the .NET world. Can’t you tell from its much more professional sounding name? It’s enterprise-friendly!

You can get started with SpecFlow in five easy steps:

  1. Download the installer from their site – they have versions for VS.NET 2008 and 2010.
  2. Start a new solution, add a reference to TechTalk.SpecFlow.dll, and add a new SpecFlowFeature file (this template will be added to Visual Studio by step 1).
  3. Define your features in natural language using Given/When/Then Syntax
  4. Add a new SpecFlowStepDefinition file. In here, you will write the code that is tied to your features based on specialized attributes (note I’m using Watin to automate IE):
    [Given(@"I have viewed the login page")]
            public void GivenIHaveViewedTheLoginPage()
                var browser = new IE();
                Assert.That(browser.Title, Is.EqualTo("Welcome!"));
  5. Build your solution and SpecFlow will automatically generate nUnit tests for you. Run your tests and behold your progress.


Just the Beginning…

This is just the beginning of what can be done with SpecFlow, but I hope it whets your appetite enough to try it out. And if you’re dying for other examples a few others have posted some details on this topic as well:

2 Comments » for Getting Started with SpecFlow in ASP.NET
  1. Normanpaterson says:

    Fantastic article, however, for an ordinary guy like me, seems like it would take time to learn and leverage it, and of course my project is due yesterday!

  2. Anonymous says:

    Yeah. This definitely falls into the category of good idea, but difficult to implement on “real” projects. In theory it would save you time later because you have tests in place and would quickly know what breaks when you change something…

2 Pings/Trackbacks for "Getting Started with SpecFlow in ASP.NET"

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>