Hanalei, Hawaii Tuesday, February 09, 2010

Kona 3: Learning Behavior Driven Development (BDD)

Holy cow! Another *DD – man I really must have an IV with that Alt.

Holy cow! Another *DD – man I really must have an IV with that Alt.NET Punch just coursing like a train in my veins! I ask for your patience with this one – cause I think BDD is a really good tool to know – whether you use it or not – knowing the workings of it will allow you to have some intelligent conversation the next time you’re in Austin :).

The Start of BDD
It all started – or so I’ve read – with a guy named Dan North:

I had a problem. While using and teaching agile practices like test-driven development (TDD) on projects in different environments, I kept coming across the same confusion and misunderstandings. Programmers wanted to know where to start, what to test and what not to test, how much to test in one go, what to call their tests, and how to understand why a test fails.

The deeper I got into TDD, the more I felt that my own journey had been less of a wax-on, wax-off process of gradual mastery than a series of blind alleys. I remember thinking “If only someone had told me that!” far more often than I thought “Wow, a door has opened.” I decided it must be possible to present TDD in a way that gets straight to the good stuff and avoids all the pitfalls.

I felt this exact way when learning up on TDD:

  1. where do I start? dog-training-dog-behavior-category
  2. what do I do next?
  3. how do I know when I get there?

Things like that. I still don’t know those answers, but I feel like BDD has helped me see some very interesting ways to address these issues. Moreover, it will help me get along with my PM better – maybe :).

The Usual Caveats 
No – I’m not an expert. Please don’t take any of what you’re about to say as guidance. Do take it as  queue to find out more. Do ask a lot of questions and Do challenge yourself to see what some people love to use.

Download it here

Double-click for full screen


ryanriv - May 15, 2009 - I really like the idea of BDD and I would love to use it. Unfortunately my organization (and myself) are *just* getting started with Unit Testing, and quite frankly I think our brains might explode if we jumped straight into BDD.

How was the transition from TDD to BDD for you? Do you think you could have started off with BDD straightaway or was doing TDD first beneficial to you?
robconery - May 15, 2009 - I would rather have skipped TDD entirely and gone straight to this - to me (and that's important... to ME) it makes a ton of sense and is easy to deal with :)
ryanriv - May 15, 2009 - Hmm, that's interesting.. I think I'm more concerned about whether my co-workers could read my tests. I know I wouldn't have a problem getting up and running with BDD, but some people don't want to, or just don't care about learning about BDD.
robconery - May 15, 2009 - ... and that's the problem. NOT that you need to do it - but you owe it to yourself to learn it :)
Liam McLennan - May 15, 2009 - Rob, I find that I need both BDD and the pure TDD style. Some tests don't require much in the way of context and are not easily considered from a behavioural perspective (because they test infrastructure) so the TDD style is more appropriate. As always, we must select and apply the right technique for the job.
njapp - May 15, 2009 - Rob, great job with the MVC Storefront and the move to Kona. Most of us appreciate your hard work and understand you'd probably rather be surfing. I really love the screencasts and appreciate the solid advice giving by all of your partners in crime, especially Ayende! [ I'm still waiting for Fowler to create a pattern to siphon Ayende's knowledge while he sleeps via Skype ] I have a quick question about the state machine you are using with your Product model, specially the CurrentInventory property. Is there a reason why the CurrentInventory setter is not marked private? Wouldn't you want this code to fail? Product p = new Product("TEST", "test", true, 10, 0, DateTime.Now.AddDays(-7), true, true); // this would put the Product in OnBackOrder p.CurrentInventory = new InStock( p ); // easily set the incorrect state FYI: All Unit Tests pass if you change the Product.Currentinventory set modifier to private and then remove the following methods from InventoryState public virtual void SetAsAvalable() public virtual void PutOnBackOrder() public virtual void PutOnPreOrder() public virtual void TakeOffline() Of course to make these changes completely functional you would have to call InventoryState.SetState whenever a Product property is set. Any thoughts?
Russell - May 15, 2009 - Rob, awesome screencast. I've had the urge to check out BDD for a couple of months now, but kept getting put off by the analyst speak everyone uses to describe it. Your screencast just cleared everything up in no time. Can't wait to give BDD a go!
Steve Willcock - May 15, 2009 - Funky delegate syntax and very 'readable' results aside I think one real benefit of BDD is the move away from using the word 'Test'. Trying to get the point of TDD across to other developers / managers is often quite difficult - often their first instinct when they see the word 'Test' in there is to think that TDD's main usage is as some kind of replacement or addendum for UAT rather than a part of the design / development process. BDD makes the distinction much clearer from the outset and reduces the potential for misunderstanding. btw, nice job on including the 'u' in behavio(u)r :)
huey - May 15, 2009 - Look forward to watching this screencast. I have yet to get all the way through a project with my tests intact. I always lose steam at a major refactoring and let things lapse. I think this is mostly because I'm writing bad tests. I also find that I don't really know where to start with TDD. Like, I'm about to start a new project, and I have a pretty good idea of what I want the service to look like ... so is it okay to start with the service and then fill out the inside? Or do you start at the inside and let it expose the service you need?
Jeffry Gonzalez - May 15, 2009 - Rob - Nice post! I enjoyed it and picked up a few things. A couple of things came to mind. First, while I think MSpec is a nice tool, I've been doing BDD-style development without it for about six months using must straight NUnit and most recently MSTest on a VB project.. I do a lot of coaching and training and it really does help with the whole "where do I get started" mentality. It is definitely a mindset and an approach, rather than a requirement for retooling. I find BDD works best in conjunction with, not as a replacement for, TDD with unit tests. My pattern? Write the specs in a test project. In another test project, write unit tests as appropriate to discover the code needed to make the specs pass. One thing that is a little lacking in your approach (and frankly, in .NET I do the same), is that specs should be written more in terms of what the user will experience. "As a customer, I want to be able to place an order so that I can receive my product and give you my money", with scenarios like "Given I am logged in, When I click the add to cart button, then the product is added to my cart and a message is displayed saying "Item added"" Because of my lacking understanding of the process of testing UI, and because of the constraints of our tools, our specs that should be more customer focused become more API focused. I'm really enjoying the Cucumber/RSpec/Webrat/Rails combination. This is an area where I really fear that the Microsoft community will try to reinvent the wheel for spec/testing. Aaron's done amazing work with MSpec, but as your screen cast showed, it sort of pales in comparison to RSpec and the expressiveness and dynamic nature of Ruby that fits so well in the test/spec story. I want to be able to use Cucumber/RSpec on ASP.NET MVC apps in the same way I use those tools on Rails apps now. I look forward to IronRuby getting up to speed. Thanks for your great work.
bakkegaard - May 15, 2009 - Rob, this is by far the best introduction to operational BDD I have seen on the net. Great work, you are an excellent and humble presenter!
Daniel - May 15, 2009 - Very nice screencast. I've looked into BDD several times and got put off by the syntax. I went into this cast with very little understanding, and came out with a good understanding of what the point is. It strikes me that it has some very nice dovetails with DDD, in that by having a human readable spec, you can more easily discuss and develop that 'common langage'. As an idea for your next DD, I'd love to see a screencast on Message Driven concepts (like RhinoBus and NServiceBus). As I did TDD, DDD, and now BDD, I have a vague understanding, but would love to see an approachable 'real world' example.
bonder - May 15, 2009 - Great screencast! I have only one question -- "groovy gravy?" ;)
bdd > tdd - May 16, 2009 - Nice presentation on BDD, but I must say that I do not like the MSpec syntax. The ruby example looked much better. Overall, a lot of good information. Thanks
Andrew - May 16, 2009 - Great screen cast, but how does one Mock anything in this style of "testing"? Or are the "Specs" going to have to hit the database if that is what the objects do?
JasonBunting - May 17, 2009 - Sorry, Rob - my inner spelling Nazi wants me to correct your spelling in one spot....When you used "queue," you probably meant "cue."

Great post regardless!
Paul Linton - May 17, 2009 - Does anyone have a link for where I can download mspec? I've been to
http://github.com/machine/machine.specifications/... but can't work out how to download. When I click the download button nothing happens. The download tabs is empty.
Aaron Jensen - May 18, 2009 - Paul, Currently the best way to get mspec is to check out the git repository and build it yourself (just run rake). I currently don't have anywhere you can download the binaries alone though I really should work on that :)
Paul Linton - May 18, 2009 - I have found that the git site works if you use Firefox. The download button just gives javascript errors with IE7 or IE8. I should have tried Firefox before posting here. This note may save someone else some time if they are trying to download from git. Thanks for responding Aaron.
Paul Linton - May 18, 2009 - Thanks for responding Aaron. The problem appears to be with IE7/IE8. The download button on the git site actually did something when I tried with Firefox. Using IE I just received javascript errors.
I don't know what 'run rake' means but opening the solution file and rebuilding the solution has created a whole bunch of dlls, which looks good.
Aaron Jensen - May 18, 2009 - Awesome, glad you were able to get it going! Sorry, rake is a ruby build tool, but yes, also opening the sln and building will do just fine. Enjoy.
mikekidder - May 18, 2009 - Holy crap that was a great screencast, I think I am actually motivated to right some specs! Great job!!
Michael Valenty - May 18, 2009 - A while back I watched a bad presentation on BDD that I pulled a good quote from. The presenter said "BDD is what you're already doing if your doing TDD right." After watching your screencast, I've changed my opinion on that statement. There are many more subtleties that I've been missing and I'm looking forward to sharing this screencast with my team at our next lunch-n-learn (I'm a nerd through and through). Lately I've been feeling guilty with how much time I'm spending getting my tests right, so today I put together some thoughts on the topic: http://mikevalenty.blogspot.com/2009/05/dont-eat-...
Bret Ferrier(runxc1) - May 18, 2009 - I love the spec idea. I am just getting into the *DD realm with testing. I think that BDD succeeds at focusing on Behaviors and specifications. I also like the readability from the test runner. I don't like the syntax of MSPEC however. It seems to me that much more could be accoumplished by just writing a description of the test. Everyone seems to prefer the Ruby syntax because it just used a good old string(Which is great for readability). Anyway it was a good screencast though I would have loved to see an implementation without MSPEC or do all BDD fans use MSPEC??
Paco - May 18, 2009 - @Bret Ferrier: You can start with something like this and extend similar classes. [TestFixture] public class ContextSpecification { protected virtual void Context() { } protected virtual void Because() { } [SetUp] public void BeforeEachTest() { Context(); Because(); } }
Paco - May 18, 2009 - @Rob / Aaron I can use mspec from the console, but I didn't manage to use mspec with testdriven.net / gallio / resharper test runner. I had a long installing/uninstalling/rebooting/diffferent versions session, but it just won't work in my machine. Where can I find a tutorial about howto install mspec?
Daniel - May 18, 2009 - I hope this isn't too much of a link-bait, but this post gave me the idea of using T4 to generate BDD-style tests from a plain-jane specification text file. Still have some work, but the proof-of-concept code has made me want to keep looking into this: http://www.danielroot.info/2009/05/spike-using-t4...
Aaron Jensen - May 18, 2009 - Paco, Rob's instructions in the screencast weren't exactly accurate, you need to put that bat file in the build/debug directory as it looks for the dll in the directory the bat file is in. As for the R# plugin, here's a blog post on how to get that running: http://blog.muonlab.com/2009/05/18/how-to-use-mac...
Paco - May 19, 2009 - Thanks a lot for the information about resharper installation. We created our first 500 testspecs today!
Michael - May 20, 2009 - You seem to have more recent source code, at least in relation to what was shown in the screencast ( referencing Machine.Specifications , not related to BDD topic: SubSonic was seen as another project in solution ) at http://github.com/robconery/Kona/tree/master is that used for work in progress and we will see at codeplex more robust version at some stage ?
robconery - May 20, 2009 - I've been having issues with source control so opted to pop stuff up to github for a short while - i'll have a post on this soon...
chriskolenko - May 21, 2009 - Hey Rob, Like always I'm not going to give you a complement i don't believe in them. You know i think you're great lol However, I do have a question. In the perfect world where your client does give you complete specs, I can see the point of BDD. How do you recommend going about endless changes of specs.?? please don't say wait till you have a complete spec.. most of my clients won't like that. They use the prototype approach, testing new ideas before making a decision. Refactor Tests?
DotnetShadow - May 21, 2009 - Hi Rob, Great screencast, I was wondering if you could explain the flow between BDD and TDD as follows: Take for example your specification that when the shopping cart is empty you add an item it should increment. As I understand you do the following: 1) Write the initial specification, at this stage there is no implementation 2) Next you would of course write a unit test for your code 3) Obviously implement the code based on your unit test 4) === MY QUESTION ===== Now I'm at the stage where I need to update my specification using MSpec "Because Of" | "It Should". What do I do? Do I take the unit test code and simply copy and paste parts of it straight into the Mspec method? DotnetShadow
robconery - May 21, 2009 - Thanks :) - so I'm not a BDD expert but from what I've learned in the course of the screencast (someone jump in if I goof up) I would:


Write the specs out, with a result of NOT IMPLEMENTED for each
Write just enough code to get each spec to pass
In the section \"It should_be_some_spec\" I'd change it to be \"It should_be_some_spec= () =>\" and then I'd write the \"cart.Items.Count.ShouldEqual(2)\" or whatever.


The \"ShouldEqual\" part here is the extension method on NUnit that IS your unit test.
chriskolenko - May 21, 2009 - Ok.. i'm now confused.. So one of my specs would be.. retrieve all categories and highlight each category which product x belongs to.. How do i hook in my ICategoryRepository and IProductRepository into a this style of coding. Everything looks static
robconery - May 22, 2009 - They're tests just like any other - so you'd want to be sure to include whatever test repositories or Mocks are required for you Context. In other words - if you need these two repos, then your Context would have an IList<Category> which you would fill from your faked or mocked repo. You would then have a Product as well. You could name your context \"with_all_categories_and_sku_1\".
JayD - May 22, 2009 - Loved this screencast. I have already started playing around with mspec. One issue that I haven't figured out is running the Spec from within TestDriven.NET. I followed your lead, and I can run the spec with MSpec Runner. However I see no way to run this and debug through the test...err... spec. Am I missing something? Also are you going to update the code on codeplex with this stuff?
Mithra - May 24, 2009 - Very informative screencast. I downloaded mspec and playing around with it. I would love to see an example which uses mock object with mspec. All examples use real objects.
cowgaR - May 26, 2009 - just watched the video (after researching BDD a bit on google videos & blogs to put me in picture) and Rob, I think I love you =) once again you proved you da screencast man! to be more serious what bugs me (I have dived deeply to it just today so sorry if it is obvious) very much is the test coverage and if I covered ALL what there was to cover. e.g. if I am unit testing (and unit is a small block), and I am starting with say entities, I am doing all sorts of micro-advancement not forgetting anything to test. With BDD it all looks like from 10 feet above so how I know there isn't some hole? hot it drives your application design (properties on entities)? how it drives the use of repositories and/or how I will test them...integration testing is now called integration specs? all sorts of questions, I'm going to relax and diggest what I've just saw...another beauty from Rob Connery, thanks! please more!
Paul Linton - May 29, 2009 - Thanks for this screencast on MSpec. Writing Specs puts me in a different frame of mind from writing unit tests. A frame of mind which seems much closer to the customer. The output produced is nice too (although I may try to give the html report a little formatting love when I get some time, it's very long winded)
I have written a few Specs and now have gone back to trying to implement the code. My code has bugs (surprise!). How do I debug? I have tried setting a breakpoint in the Because delegate before the place where the exception is thrown but execution does not stop when I use the mspec runner. I have tried breakpoints in all sorts of places but they are never triggered. Is there a qualifer to the runner that I am missing, or is this just a fact of life?

(Sorry if this post appears twice - intenseDebate said that they lost my comment the first time)
John - June 3, 2009 - OK, anyone having issues download mspec. I downloaded it from git. but I can't open it without and errors let alone compile. I get a ton of filename and/or path is to long. My path cant be shorter (I downloaded it to the root). any ideas?
Larry - June 9, 2009 - So I take it the MVC Storefront is dead?
robconery - June 9, 2009 - No
Chris - June 22, 2009 - Hmmm. This has turned my development attitude on it's head!
Nik - July 24, 2009 - Why not use MSpec to write your specifications, you then have your spec failing, after which you use standard TDD to explore solving your spec? Maybe?
Jeff - August 10, 2009 - if it isn't dead, then where is it and where is it going? will it be finished and put into a new starter kit?
Kelly - August 18, 2009 - Dude - I've really enjoyed following this. Once again you've given the dev community something really valuable.

Perhaps, though, now would be a good time to lay out where Kona is going. Will it be another Commerce Starter Kit? Is it simply a tool to explore MVC, TDD, BDD, etc...? Something else? Really, any of the above is a worthwhile experience.

Its not like you're busy or anything :-) BTW, congrats on SubSonic 3 and MVC 1.0 (a little late on both - sorry). They both rock!
Bob Armour - August 23, 2009 - I won't comment on the 'behaviour' vs 'behavior' debate (See my surname to understand why), but will question the use of 'queue' instead 'cue', in the caveat section ;-)

Seriously, though, it's an interesting presentation - now I just need to work out how appropriate it is to our way of working, and whther I can convince my colleagues to consider using it.
rkay - September 4, 2009 - I've been following the storefront webcast very eagerly and watched it like a child growing. Sure i learnt a hell lot of things but i really wanted an ecommerce application - not off the shelf - but something i understand and extend/modify as i need. Unfortuntely the end was not pleasant. I don't know what's happening with Kona as there's not much movement i could see. Could you please update us Rob about what's on your mind. Meanwhile what's the last release of source code just before Kona and which was still MVC Storefront. How complete it is. I appreciate your efforts and i wish you doing more webcasts. Thanks.
James Coleman - September 14, 2009 - Yeah, I am with you rkay. I have been really busy with work so I haven't viewed Rob's blog in quite sometime, but I am catching up now. Now that he has left Microsoft, I am thinking Kona probably won't happen or be in a shape that resembles the screencasts. I also read today a comment from Rob in a different post:

"I didn’t “quit” Kona either – it belongs to the team I worked on. I can dig you didn’t like it – I didn’t like it much either."

The last part of the quote scares me a bit because I am going live soon with a site that used the MVC Storefront as the starting point of the site. I thought the screencasts were great and learned a lot. This site that is going live is based on the MVC Storefront but I changed it quite a bit - for we are selling AND BUYING items and there were some things I just didn't like and I changed how they were coded. I am just hoping that the little things that I didn't like are the same things that Rob "didn't like".

Rob would it be frowned upon by Microsoft if you did a post that was just a high-level of "If I knew then what I know now" in regards to the MVC Storefront.
Rob Conery - September 14, 2009 - What I didn't like were some of the spikes I did in Kona - specifically the Data Access stuff. I had an idea and... well it didn't work. That's what spikes are for :).

I also think I went overboard on the CMS stuff - but it's OK, I was experimenting.

I liked the Storefront bits - I'll hopefully pick that up again in the future. Bertrand and some of the ASP team are running with Kona now.
bearing09 - September 17, 2009 - It's great to hear Rob's reply of kona, I just really miss the kona bits.
Gecko