Step by Step BDD with ASP.NET MVC 4 SpecFlow And MsTest – Part 1

In this first post I’m going to show a step-by-step example to illustrate the BDD (Behavior Driven Development) process using Visual Studio 2010, MsTest and SpecFlow to create Acceptance Tests for an MVC4 Web Application.

I assume you already have installed a decent version of Visual Studio 2010 in your box that allows you to create MVC4 Web Applications and run unit tests using MsTest, the Microsoft’s Unit Tests framework.

To simplify, I’m going to use the default MVC application created through the Visual Studio template. By adding an Acceptance Test, I’m going to verify that a controller method behaves as expected.

1 – Create an MVC4 Web Application

Create new MVC4 Web Application
Create new MVC4 Web Application

In the Application Settings Dialog, check the option to create a unit test project and select Visual Studio Unit Test Framework.

Create the MVC App with a Unit Test based on the Microsoft Framework
MVC4 Application with Unit Test Project Creation Wizard

Your Home Controller should look like this

Default Home Controller
Default Home Controller

2 – Install SpecFlow Visual Studio Integration

Download and Install the SpecFlow Integration Tool from the Visual Studio Gallery

This extention will enable the SpecFlow Item Templates to add .feature and step difinition files in the Acceptance Test Project.

SpecFlow Item Templates
SpecFlow Item Templates

3 – Add an Acceptance Tests Project

I’m not going to use the Unit Test Project created in step one. It should be used just to support Unit Tests, which are different from the Acceptance Tests I’m creating.

But lets analyze this difference in another post and create the Acceptance Test Project by right clicking the Solution Explorer and selecting Add -> New Project and selecting Test Project template as shown in the image below.

Add a new project to the solution
Add new project to the solution
Select Test Project Template
Select Test Project Template

3.1 – Reference SpecFlow in the Acceptance Tests Project

To make the Acceptance Tests work, I need to reference the SpecFlow library in the new project by simply adding the Package reference from the NuGet Package Manager

Install SpecFlow NuGet Package

3.2 – Setup MsTest as your preferred Unit Test Framework

SpecFlow can work with NUnit and MsTest. In our case, I’m going to set it up to run the Acceptance Tests using MsTest.

Just add an App.config configuration file in the Acceptance Test Project and set MsTest as the Unit Test Provider.

As you can see, the only thing to do is adding this line to the App.config file

unitTestProvider name=”MsTest”

SpecFlow will re-generate any code behind files (.feature.cs) automatically to make use of MsTest.

Set MsTest As Unit Test Provider
Configure the Unit Test Framework to run Acceptance Tests

For additional information see the SpecFlow Configuration Document

At this point your Visual Studio and SpecFlow integration is ready and you have created an MVC4 application containing an Acceptance Tests Project.

The second part shows how to follow the BDD process to implement an scenario.

Leave a Reply