Behavior-Driven Development with NBehave

What it Behavior-Driven Development (BDD)?

This new development technique is an evolution of the well-known Test-Driven Development. It encapsulates the idea of the Domain Driven Design to use a Ubiquitous language which should break the boundary between Business and Technology. That way, BDD makes  sure that the business, developers, testers, analysts and managers use the same words with equal meaning

The BDD process looks like this:

A business user works with a business analyst to identify a business requirement. This is expressed as a story using the following template:

  • As a Role
  • I request a Feature
  • To gain a Benefit

The speaker, who holds the Role, is the person who will gain the Benefit from the requested Feature. Dan North has written a great article about stories. I advice you to read it if you feel that it’s not clear enough.

Read more about this at BDD wiki.

NBehave

This is a .NET framework, which helps you do BDD. It’s very simple and powerful. Here is what its authors say about NBehave:

Based on Dan North’s initial vision of rbehave and utilizing the behavioral domain specific language (DSL)of Behavior Driven Design (BDD) we created the NBehave framework. The primary goal of NBehave is a framework for defining and executing application requirement goals. These characterizations are modeled after the Behavioral Driven Design (BDD) terms Story, Scenario, Given, When, Then. Relying on a syntax that is lightweight and targeted at product owners (a few “quotes” mostly), the code becomes an executable and self-describing requirements document. The definitions within the actual unit test of the application coupled with the organic nature of the architecture and ubiquity of the domain model translates these concepts into becoming one cohesive amalgam. With the help of Domain Driven Design, the code actually becomes what we have always wanted, living requirements that are constantly asserted on to ensure their viability and accuracy from inception to implementation. Can you say true traceability!

In Action

First, download the latest release of the framework from CodePlex. It supports most of the popular test frameworks fro .NET:

  • NUnit 2.5.2
  • xUnit 1.5
  • MbUnit 3.1.3.313

The first important thing are scenario files. They describe your story with all its scenarios for testing. A simple scenario file looks like that:

First you define your scenario. A scenario represents a real situation you want to test. You may also include information for the whole story. Read more about scenario filesin NBehave.

The second step is to create a connection between your scenario file and your test file. Create a new project of type Class Library. Add reference to NBehave.Narrator.Framework, NBehave.Spec.Framework, NBehave.Spec.NUnit (or any other of the available test framework specifications) and NUnit.Framework (or your favorite test framework).

Next step is to create a new class. Make it derive from NBehave.Spec.NUnit.SpecBase. Each test framework specification assembly contains such base class. It is marked with appropriate attributes to make its children test classes.

The link between the scenario file and the test class is made with attributes. Did I suprised you? NBehave supports three attributes: GivenAttribute, WhenAttribute and ThenAttribute. Each of them supports input as regular expression in order to extract the data and pass it to the paramters of the corresponsing method. Here is what I mean:

NBehave parses your scenario file and match each line with a method, marked with the appropriate attribute. This way you have three methods which define the logic of your test.

Run It

You run your test using  NBehave console, which ships with NBehave.

NBehave console output

You can download the demo application and investigate it yourself.

Useful links