Integrating nBehave with nUnit, MbUnit or Gallio

On a lot of testing frameworks/runners I read   integration. But I found no examples. But then it just came to me it’s actually very very simple. So after banging my head against the wall a few times and then a few times more for being stupid, I thought I should share this with others. Please let me know that you found this helpful so I feel less stupid.

Let’s start with a specification example for a repository service:

[Theme("Storing and Retrieving Data Objects from the repository.")]
public class RepositorySpecs
{
    [Story]
    public void StoreObjectInRepository()
    {
        Story storeObjectStory = 
		new Story("Store and retrieve an object in the repository");

        storeObjectStory.AsA("service in the platform")
                        .IWant("to persist data")
                        .SoThat("I can retrieve this later");

        IRepository repository = null;
        object dto = null;

        storeObjectStory.WithScenario("a flat data object whitout IStorable")
            .Given("the data object is a $type",
		"singleLevelDto",
		dtoType => { dto = DtoFactory.GenerateDto(dtoType); })
            .And("a IRepository service",
		() => repository = Service.Get<IRepository>())
            .When("I store the object under $id and the default datastore",
		"SimpleDtoTest",
		objectId => { repository.Store(string.Empty, objectId, dto); })
            .Then("I should be able to retrieve it by supplying $id and the type",
		"SimpleDtoTest",
		objectId => { repository
		    .Retrieve<SingleLevelSimpleDto>(string.Empty, objectId)
		    .PropertyValuesAreEqual(dto); });
    }
}

We can easily change the specification in an test and still keep the specification descriptive format. Let’s start by adding the following using statements to the top of the file:

using That = NUnit.Framework.TestAttribute;
using Should = NUnit.Framework.DescriptionAttribute;
using Context = NUnit.Framework.TestFixtureAttribute;
using Specification = NUnit.Framework.TestAttribute;
using Concerning = NUnit.Framework.CategoryAttribute;

After using these attributes our example now looks like this:

[Theme("Storing and Retrieving Data Objects from the repository."),
 Context,
 Concerning("IRepository")]
public class RepositorySpecs
{
    [Story, That, Should("Store an Object in the repository")]
    public void StoreObjectInRepository() 
    { 
        Story storeObjectStory = new Story("Store and retrieve an object in the repository"); 

        storeObjectStory.AsA("service in the platform") 

This is now a fully functioning and valid NUnit test, recognized by resharper (if you’re not using this tool you are really missing out on productivity).

resharper example

Of course NUnit recognizes it too:

image

There are still two things that bug me:

  • I am duplicating the text in the Should attribute and the Story constructor.
  • you miss out on a lot of the BDD output. But that’s what dryrun is for I suppose.

But for now I’ll be able to live with both since I can easily debug from within visual studio (I know I am easy to please :) )

Tags: , , , , , , ,

Comments

commercial roofing hayward
commercial roofing hayward United States
3/3/2010 12:31:14 PM Permalink

I just came by to say thx for this post

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading