Wednesday, 10 September 2008

First play with NBehave

I've been reading about Behaviour Driven Development and the tools around it, namely NBehave.

Behaviour Driven Development (Ok I'm going to call it BDD from here on ...) looks to be a great way of making the developer keep their focus on what their objectives are for the code their about to write. Test Driven Development (TDD) was the start of that, but it BDD takes it a step further and relates tests to the business value they're trying to achieve. I think every developer is guilty of jumping into code and forgetting where they came from or why they're there at some point.

I like code that reads closer to English, the sentence style of NBehave makes it easy to read the code and flow through and provides structure.

Dan North's article What's in a story? is a great introduction into the structure of user stories, and is worth a read even if BDD isn't something you're considering.

You can download NBehave from CodePlex and I'm using MbUnit as my testing framework, but I guess any other would work just as well.

Some Code...
Here's the source code for the quick example I wrote:-

The Test Code
Here's the testing code:-
    [Theme("Calculator"), TestFixture]
public class

[Story, Test]
public void Should_Add_Up_Positive_Numbers()
//create the instance being used in testing
Calc c = new Calc();

//create vars for test input
int a = 0;
int b = 0;

//create var that will have result filled
int result = 0;

//create the user story being tested
Story s = new Story("Should add up positive numbers");
.IWant("to have numbers add up")
.SoThat("I can get the total");

//setup scenario conditions and post conditions
s.WithScenario("Both numbers are positive")
.Given("First Number is positive", delegate { a = 5; })
.And("Second Number is positive", delegate { b = 6; })
.When("Numbers are added up", delegate { result = c.Add(a, b); })
.Then("Total is correct", delegate { Assert.AreEqual(11, result); });


The Production Code
Here's the production code to make the unit test pass:-
     /// <summary>
Sample calculator class
/// </summary>
public class
/// <summary>
Adds the specified a to b and returns result.
/// </summary>
public int Add(int a, int b)
return a + b;
Test Output
I like way the test code reads just like a user story definition, it prints out in the test output which could be useful for creating testing reports.
------ Test started: Assembly: NBehaveTesting.dll ------

Starting the MbUnit Test Execution
Exploring NBehaveTesting, Version=, Culture=neutral, PublicKeyToken=null
MbUnit 1.0.2700.29885 Addin
Found 1 tests
Story: Should add up positive numbers

As a Administrator
I want to have numbers add up
So that I can get the total

Scenario 1: Both numbers are positive
Given First Number is positive
And Second Number is positive
When Numbers are added up
Then Total is correct
[success] Class1.Should_Add_Up_Positive_Numbers
[reports] generating HTML report
TestResults: file:///C:/Users/peter.beams/AppData/Roaming/MbUnit/Reports/NBehaveTesting.Tests.html

1 passed, 0 failed, 0 skipped, took 2.60 seconds.

I hope this first example is useful to someone taking a look at NBehave. I'll be looking at BDD and NBehave in more detail soon, so more posts to follow. I'm sure there are some war stories of using NBehave in large projects, so I'm hunting for them!