BDD with nbehave tips

While using nBehave on projects especially those where we collaborated with external partners I created/shared the following list of guidelines/tips which might save you some time too. So here I am sharing it to a broader audience… maybe :)

1. All declarations/instantiations must be placed after the narrative
2. All code that can fail must be inside an actionstep delegate or lambda
3. How to execute and debug using nBehave Console Runner
4. Improving execution and debugging by integrating with nUnit

All declarations/instantiations must be placed after the narrative

The first two solve the same problem. When code fails that’s not inside an actionstep it is unhandled (especially with the nbehaverunner, look at 4 for a different runner) and the whole thing just stops with an exception. What’s even worse invalid/incomplete xml is written that will break any xslt transformation or processing which I found out the hard way while maintaining a CI (). How to integrate nBehave (or rather how I integrated it) with Hudson is worth a post on it’s ownn that will follow.

uses the Story class to execute and listen to output of the specification. A very important consequence is that if code that is not part of an actionstep (inside a lambda or delegate as a parameter of an actionstep) the complete test fails with no or invalid output.

A lot of us would write a spec in this way (since we are used to put our declarations at the beginning of a code block:

IRepository repository = Service.Get<IRepository>();

Story storeObjectStory = new Story("Store and retrieve an object in the repository");

When the Service.Get fails the story will never be created and we have no output on why or which test failed.

Our first guideline is to move all declarations past the story narrative. We created a modified nBehave runner build that produces valid output even when the code throws an exception (if you’re interested let me know).

Let's look at our modified example:

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 = Service.Get<IRepository>();

storeObjectStory.WithScenario("the single level data object does not implement IStorable")

All code that can fail must be inside an actionstep delegate or lambda

nBehave will nicely catch and report exceptions in code that are part of an actionstep. Code failing outside an actionstep will result in unexpected behaviour.

To improve our previous example we should place the initialization of the repository component inside an actionstep. Since it is part of the setup the appropriate place is the “Given”-actionstep (or a subsequent “And”-actionstep).

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("the single level data object does not implement 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); });

 

How to execute and debug using nBehave Console Runner

A major pain point of nBehave is the difficulty of testing it on a local machine. One way of executing these is by creating or re-using the batch scripts used by your CI-server (in our case it’s hudson). We use a tools/scripts folder in our source tree.

We have a nBehaveRunAll.cmd which will execute all component specs. It accepts an output directory parameter relative to the different project folders. By default it uses the output of the debug (anycpu) configuarion (the configuration chosen in visual studio). The following example shows how to specify the release configuration:

... >Tools\Scripts\nBehaveRunAll.cmd bin\Release 

To debug you can specify the nbehave console runner in your project properties:

image

In Start External Program fill in the path to the NBehave console runner.

The command line arguments consist of 2 arguments:

  • The assembly you want to run
  • xml=<xml output> parameter. This one is important. Without it you are not executing the tests.

Now you can set breakpoints and start debugging by pressing F5 :)

Keep in mind that these settings are configuration dependent! This means that whenever you change the build configuration (for instance to release or x86 instead of any cpu) you’ll need to reenter these values. Is there no better way you ask? Read on to find out how…

Improving execution and debugging by integrating with NUnit

The above is fine if you are a build server but a lot of developers use resharper or the (or the Galio Icarus) GUI to run and debug tests. Changing my specs into this made such an impact on the usage of NBehave with our partners, that I created a seperate blog post explaining this. Before a lot of the partners “forgot” to execute, update or correct the executable specifications (BDD tests) because they perceived them as being hard and time consuming. By changing them into NUnit tests this problem was solved.

Tags: , , , , , , , ,

Comments

payday loans
payday loans United States
11/28/2009 9:00:45 PM Permalink

Searching for this for some time now - i guess luck is more advanced than search engines Smile

online payday loans
online payday loans United States
1/27/2010 10:19:06 AM Permalink

Leadership is practiced not so much in words as in attitude and in actions.

cash advance
cash advance United States
2/4/2010 7:19:33 PM Permalink

There is no victory at bargain basement prices.

acai berry
acai berry United States
2/13/2010 11:37:31 AM Permalink

The key to successful leadership is influence, not authority.

no fax payday loans
no fax payday loans United States
2/14/2010 2:03:05 PM Permalink

To solve any problem, here are three questions to ask yourself: First, what could I do? Second, what could I read? And third, who could I ask?

Jule Gehr
Jule Gehr United States
2/18/2010 7:46:46 PM Permalink

Without doubt agree with what you stated. Your explanation was by far the simplest to understand. I say to you, I often get irked when folks discuss stuff that they clearly have no clue about. You managed to hit the nail right on the head and also talked out everything without complication. I hope, others can take a cue. Will likely be back for more. Keep writing

Marc Dedrick
Marc Dedrick United States
2/19/2010 5:29:57 AM Permalink

Hi, good day.. Your post is very uplifting. I never thought that it was probable to accomplish something like that until after I read your post. You undoubtedly gave an excellent perception on how this kind of whole process works. I will always return for more information. Thanks!

Ross Huhtasaari
Ross Huhtasaari
2/19/2010 7:51:47 AM Permalink

Hi, great day.. Your content is quite striking. I never considered that it was possible to do something like that until after I looked over your post. You certainly gave an excellent understanding on exactly how this whole system works. Ill make sure to come back for more information. Keep writing!

stock charts
stock charts United States
2/20/2010 9:08:47 AM Permalink

Do you mind if I quote you on my blog if I link back to your website?

Daniel Millions
Daniel Millions United States
2/20/2010 11:44:41 AM Permalink

Awesome blog it's not often that I comment but I felt you deserve it.

1 hour payday loan
1 hour payday loan United States
2/21/2010 3:27:54 AM Permalink

Can I quote you on my blog if I link back to your website?

Best Calling Card
Best Calling Card United States
2/22/2010 3:14:18 AM Permalink

Can I use an excerpt from your post on my blog? I think my readers would appreciate it. I will give you a link back so you can get credit. let me know please.

fishing in key west
fishing in key west United States
2/24/2010 1:19:52 PM Permalink

If you are looking for information on fishing in Key West or Key West fishing guides, check out this site- http://www.keywestcustomcharters.com Tight lines!

stock prices
stock prices United States
2/25/2010 2:15:20 AM Permalink

Just wanted to say that you have some great content on your blog. If it's allowed I would like to use some of the information you provided on my webiste. If I link back to your website do I have your permission to do so?

Daniel Millions
Daniel Millions United States
2/25/2010 3:49:28 AM Permalink

Can I quote you on my blog if I link back to this page?

Stretch Mark Removal
Stretch Mark Removal Taiwan
2/26/2010 12:17:18 PM Permalink

Hi
I feel enjoy at your blog. Your article very helpful for me. Keep writing and happy blogging.

Thanks
Dave

Massage
Massage United States
2/27/2010 4:56:20 PM Permalink

Fascinating read, possibly the best article iv'e read today. We learn everyday many thanks to you!

debt consolidation loans
debt consolidation loans United States
3/2/2010 4:15:56 AM Permalink

I didn't see a link anywhere but do you allow advertising? I have several blogs in related niche and I would like to add my banner somwhere on your page.

Forest Bolstad
Forest Bolstad United States
3/2/2010 10:09:58 AM Permalink

Here it looks like there is a code error. All i see is a bunch of code in where the article should be. Is this mine or your problem?

Inocencia Gosserand
Inocencia Gosserand United States
3/2/2010 10:22:50 AM Permalink

Here it looks like there is a code error. All i see is a bunch of code in where the article should be. Is this mine or your problem?

Kandis Hassan
Kandis Hassan United States
3/2/2010 10:42:42 AM Permalink

Pretty interesting post. Couldn't be written any better. Browsing this post reminds me of my old friend. He always kept talking about this. I will send this post to him. Am sure he will have a good read. Thanks for sharing! Smile

Daniel Millions
Daniel Millions United States
3/2/2010 11:46:36 AM Permalink

Great post I bookmared it on Delicious and submitted on Digg. Hopefully it sends more people your way Smile

stocks
stocks United States
3/2/2010 9:42:58 PM Permalink

If you are open to having a guest blog poster please  let me know. I will provide  unique content for your webstie, thanks.

stock trading
stock trading United States
3/3/2010 12:04:37 AM Permalink

Great Post, I love to read articles that are informative and beneficial in nature. Thank you for sharing your experiences and I look forward to reading more.

Can I quote you on my website if I link back to your blog?

cash advance
cash advance United States
3/3/2010 8:04:02 AM Permalink

Most of the time I don’t make comments on websites, but I'd like to say that this post really made me to do so. Really nice post!

Phangan Hotels
Phangan Hotels Germany
3/4/2010 8:08:10 AM Permalink

Hi mate
Nice post.
One more awesome blog Google showed me. Subscribed to the RSS
I will be back here soon again

Stevie Seiner
Stevie Seiner United States
3/5/2010 6:20:12 AM Permalink

It is much simpler to post about matters that run good, or looked good on the outside. When we flipped to Extreme Programming a few ages ago, we made outstanding errors assuming that automated testing was adequate, and shipped several sorry releases. We determined to a greater extent around testing (and how to better it) from those bad releases than from the succeeding satisfactory releases.

moon in my room
moon in my room United States
3/5/2010 12:05:26 PM Permalink

Thanks for taking this opportunity to talk about this, I feel strongly about it and I benefit from learning about this subject. If possible, as you gain data, please update this blog with new information. I have found it extremely useful.

ELC
ELC United Kingdom
3/6/2010 9:52:57 AM Permalink

This is my first time I have visited this site. I found a lot of interesting information in your blog. From the tons of comments on your articles, I guess I am not the only one! keep up the great work.

Daniel Millions
Daniel Millions United States
3/6/2010 11:42:34 AM Permalink

I've been here a couple times and it seems like your articles get more informative every time. Keep it up I enjoy reading them.

stock prices
stock prices United States
3/7/2010 12:51:23 AM Permalink

If you are open to having a guest blog poster please reply and let me know. I will provide  unique content for your webstie, thanks.

Amelia Mullis
Amelia Mullis United States
3/7/2010 3:08:26 AM Permalink

Although I would’ve preferred if you went into a little bit more detail, I still got the gist of what you meant. I agree with it. It might not be a popular idea, but it makes sense. Will definitely come back for more of this. Great work

Sid Lesches
Sid Lesches United States
3/7/2010 6:11:55 AM Permalink

Completely understand what your stance in this matter. Although I would disagree on some of the finer details, I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks

Yong Croke
Yong Croke People's Republic of China
3/7/2010 10:29:33 PM Permalink

Hi, Thanks. Having been going through the same issue. Resolved now Regards.

Kartenlegen per email
Kartenlegen per email Germany
3/9/2010 10:12:03 PM Permalink

Good Info. Tweeted about it. I'll bookmark this post too later.

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading