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
In the Application Settings Dialog, check the option to create a unit test project and select Visual Studio Unit Test Framework.
This extention will enable the SpecFlow Item Templates to add .feature and step difinition files in the Acceptance Test Project.
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.
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
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
SpecFlow will re-generate any code behind files (.feature.cs) automatically to make use of MsTest.
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
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
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
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.
The Steps Definition Wizard will show the steps to be generated. Click on “Generate” to create the Steps Class.
1.3 – Make your test fail
Your Steps class should look like this
public class HomePageSteps
[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;
Not only you’ll be able to build your application without problems, but also you will see your test running and failing (As expected).
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!";
2.2 – Make the test pass
Now build the solution and run the test again to see it passing.
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.
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.