Integrating nBehave with nUnit, MbUnit or Gallio

by Koen 6. November 2009 04:34

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: , , , , , , ,

BDD | NBehave | NUnit

Comments

5/17/2010 6:38:12 AM #

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        Document Moved
        [williame.be]
        on Topsy.com

topsy.com | Reply

7/6/2010 1:46:28 PM #

pingback

Pingback from isaac63bradshaw.edublogs.org

Tips to get Better Top to bottom Bounce Prime  Techniques | isaac63bradshaw's blog

isaac63bradshaw.edublogs.org | Reply

7/6/2010 4:26:40 PM #

pingback

Pingback from daniel822rach.wordpress.com

Tips to get More Up and down Jump Top rated  Tactics « Tips to get More Vertical Be quick Major  Procedur

daniel822rach.wordpress.com | Reply

7/6/2010 4:34:15 PM #

pingback

Pingback from hans92dinnerman.wordpress.com

Tips to get Excessive Vertical Get going Top notch  Strategies « How to get a Excessive Directory Start Best  Tacti

hans92dinnerman.wordpress.com | Reply

9/11/2010 8:42:28 AM #

chris langham

Seriously, please dump the automatic comments here on your site…

chris langham United States | Reply

12/12/2010 6:45:03 AM #

khan vs maidana full replay

this blog is so very interesting., i love reading it., thanks for the information here....

khan vs maidana full replay United States | Reply

3/24/2011 6:04:38 AM #

pacquiao vs mosley

hey.... your blog is really awesome i think you should always updated... thanks

pacquiao vs mosley United States | Reply

4/5/2011 3:09:41 PM #

pacquiao vs mosley

This blog is really Cool! You can visit my site for the upcoming fight of Pacquiao vs Mosley.   pacquiao-vs-mosley-live-stream-fight.blogspot.com/.../pacquaio-vs-mosley-live-streaming.html

pacquiao vs mosley United States | Reply

4/8/2011 1:50:10 AM #

Copywriting Services

This site was referred to me and I'm glad I visited it. Well done.

Copywriting Services United States | Reply

4/8/2011 1:51:04 AM #

Copywriter

This site was referred to me and I'm glad I visited it. Well done.

Copywriter United States | Reply

6/12/2011 5:15:10 AM #

Smith

Valuable info. Exactly what I was looking for. i'll check back soon again.

Smith United States | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading