Category Archives: Test Driven Development

Whitepaper: Using BDD To Make Software Development Project More Successful

You can checkout the whitepaper we wrote for Belatrix Software from here or from the link below.

It describes how communication problems could be dramatically reduced by specifying collaboratively “What the software needs to do” to be acceptable from the user’s perspective.

Hope you like it!

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.

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

In part one I showed how to integrate Visual Studio with SpecFlow and how to setup MsTest as our preferred test framework.

In this post we are gonna implement a basic functionality following the BDD principles.

1 – Write a failing test

Firstly, we need to specify “what” the system needs to “do”. And this specification is written in
Gherkin Language

Feature: HomePage
	In order to access the best mvc application in the world
	As a web user
	I want to access the home page

Scenario: Default Homepage Message 
When The User Is In The Homepage
Then The Page Title should say "Welcome to this wonderful site!"

1.1 – Add a .feature file

Add a Feature File
Add a Feature File

The new .feature file contains a default text that comes with the template. Lets replace its content with our new feature and scenario as follows

Your feature should look like this
The new feature file ready to start the test implementation.

1.2 – Add the step definitions

Now that we have told “what” the system needs to do, we need to add the necessary code to make the acceptance test work.

This is done by adding an Steps Class, which contains one method per step. As the scenario we are working on contains 2 steps, the corresponding class will have 2 test methods.

SpecFlow’s Visual Studio Integration provides a context menu to create all this stuff for us.

Choose Generate Steps Definition from the Context Menu
Choose Generate Steps Definition from the Context Menu

The Steps Definition Wizard will show the steps to be generated. Click on “Generate” to create the Steps Class.

Click on Generate to create the steps class.
Click on Generate to create the steps class.

1.3 – Make your test fail

Your Steps class should look like this

    public class HomePageSteps
        ActionResult _actionResult;
        HomeController _controller;
        [When(@"The User Is In The Homepage")]
        public void WhenTheUserIsInTheHomepage()
            _controller = new HomeController();
            _actionResult = _controller.Index();
        [Then(@"I should see a Message that says ""(.*)""")]
        public void ThenIShouldSeeAMessageThatSays(string p0)
            var viewBag = (_actionResult as ViewResult).ViewBag;
            var actual = viewBag.Message;

            Assert.AreEqual(p0, actual);

Not only you’ll be able to build your application without problems, but also you will see your test running and failing (As expected).

Run the test to see how it fails
Run the test to see how it fails

2 – Implement the necessary changes to make the test pass

Notice that at this point we did not write a single line of implementation code. The only one thing we have completed is the Acceptance Test.

So, the next step is to write enough code to make the test pass.

2.1 – Write the implementation code

Go to the HomeController and change the message.

    public class HomeController : Controller
        public ActionResult Index()
            ViewBag.Message = "Welcome to this wonderful site!";

            return View();

2.2 – Make the test pass

Now build the solution and run the test again to see it passing.

Run the test again to verify it passes
Run the test again to verify it passes

3 – Refactor

Congrats! You’re done.

In more complex scenarios, this phase would be useful to clean up your code, make it easier to test, easier to maintain, etc.

Thanks for reading!


Principles of BDD at Wikipedia

Gherkin Language

Webinar: How can Behavior Driven Development make you more successful?

This is the English Recording Version of the BDD Webinar, I presented with Mauricio Bessé for Belatrix Software.

In this presentation we talked about how communication problems may affect software quality and how Behavior Driven Development can help to address them.

There is also a quick practical example, where I show the BDD process stages.

Hope you like it!

Webinar: Cómo Behavior Driven Development hace más exitoso un proyecto?

Esta es la versión en Español del Webinar sobre BDD que presenté con Mauricio Bessé para Belatrix Software el pasado mes de Agosto.
En la presentación hablamos sobre cómo los problemas de comunicación afectan la calidad del software y cómo Behavior Driven Development puede ayudar a resolverlos (o al menos minimizarlos).

Al final de la presentación se muestra un sencillo caso práctico para ilustrar las etapas del proceso BDD.

Espero les guste.