Visiblox » Archive for November 2009

Archive for November 2009

Behaviour Driven Development for .NET developers

Wednesday, November 11th, 2009

On the Scot ALT.NET meeting held on 4th November Robert Lewis gave a talk on BDD explaining the basics, motivations and demonstrating the .NET tools available to support this methodology. I found the talk to be really interesting, mostly because Robert was focusing on the practical side of BDD – that is how .NET developers can use this methodology and at what projects might this be beneficial.

What is BDD?

If you haven’t heard of BDD yet, Dan North’s Introducing BDD or the BDD summary on Wikipedia is a good place to start. In a nutshell BDD is making another step of abstraction from TDD with the help of Domain Driven Design.

Instead of specifying atomic level unit tests, it tries to describe the desired behaviour of the software with natural language sentences. These sentences can then be written by the non-technical end users allowing them to get involved in the testing process. The sentences – often referred to as user scenarios – are then implemented by the developer as a series of unit tests.

BDD Frameworks for .NET

There are currently two BDD frameworks .NET developers can use to implement this methodology: NBehave and the IronRuby compiled version of Cucumber.

NBehave is a .NET version inspired by Dan North’s JBehave. It basically allows user defined stories in a text file to be mapped to a series of test functions. For example the sentence “Given I have logged in to the system, when I start a new transaction, then I will have to enter my password” can be mapped to the test functions:

[ActionMethod]
public void Given_I_have_logged_in_to_the_system()
 
[ActionMethod]
public void When_I_start_a_new_transaction()
 
[ActionMethod]
public void I_will_have_to_enter_my_password()

The mapping can be be done in a more readable way, by having attributes containing the mapping strings e.g.

[ActionStep("I have logged in to the system")]
public void Login()

The framework goes through the user defined sentences, does the mapping and outputs whether the user stories have failed or succeeded. This way BDD can be implemented via the Red-Green-Refactor cycle which is familiar to ones using TDD.

NBehave has support for using regex expressions when mapping user stories and test functions which gives it a larger flexibility compared to having to define individual test methods for every test. It’s also worth noting that it supports most .NET unit testing libraries suggesting integration into existing projects using these libraries is smooth.

The main drawbacks of the framework are that it’s not really documented and there’s no support for defining the same user stories with different parameters – you have to write them down again each.

Cucumber is a Ruby based tool which seems to be better documented and more feature-rich than NBehave (e.g. having the support for defining parameters for a given user story in a table-like format). Thanks to the IronRuby implementation it can be compiled to run on .NET.

(more…)


Advanced Data Visualisation - Powered by Microsoft® Silverlight™ and WPF
© Scott Logic Limited 2011. All rights reserved. Terms of Use | About Us