Jan 05 2010

BDD in .NET with Cucumber part 3: Scenario outlines and tabular templates

Published by gojko at 7:26 am under articles

In part 2 of this tutorial we used tables to organise scenario information efficiently. This works fine for processing lists of objects, but it requires additional code to work with Cucumber tables and it isn’t the best solution when things go wrong. In this part, I’ll show you how to save even more time and effort when working with repetitive scenarios.

What’s wrong with plain tables?

Try this yourself: modify the expected scenario from the previous part to make it fail and re-run Cuke4Nuke. You’ll see that there’s something wrong, but what exactly? “Tables don’t match” isn’t the most helpful error message, especially when you’re dealing with a long list of objects.

Using scenario outlines

Luckily, Cucumber allows us to specify a scenario template and then a list of parameters for the template in a table. Instead of Scenario, we use the Scenario outline header. Parameters in the template should be enclosed in in the less-than < and greater-than > symbols. After the scenario, we list the examples in the table under the Examples header.

	Scenario Outline: Hello World (with examples)
		Given The Action is <action>
		When The Subject is <subject>
		Then The Greeting is <greeting>.

		Examples:
		|action|subject|greeting|
		|Hello|Mike|Hello, Mike|
		|Jump|Tom|Jump, Tom|
		|Shout|Jim|Shout, Jim|


Cucumber will execute the scenario once for each row of the Examples table. This is effectively the same as specifying the same scenario three times with different data, but a lot less verbose and easier to understand. As we’re using the same scenario steps as in part 1 of this tutorial, there is no new C# code and we can just run the whole thing again.

Try to break it again – modify one scenario example to fail. You’ll see a much better error report than with table arguments. The NUnit exception stack trace isn’t something I’d especially like to show to a business user, but Cuke4Nuke points us to the exact row and describes the failure in detail:

Scenario outlines allow us to re-run the same scenario steps several times for different parameter values, without special code to handle tables and with better error reporting. For further examples, see Richard Lawrence’s post on removing duplication using scenario outlines.

You can get the source code for all the examples mentioned in this tutorial from GitHub Cuke4NukeExample repository.

If you’re interested in learning more about Behaviour-Driven Development and Cucumber, check out my new BDD workshops.


Get notified when I post something new - subscribe via RSS or Twitter!

3 responses so far

3 Responses to “BDD in .NET with Cucumber part 3: Scenario outlines and tabular templates”

  1. Aslak Hellesøyon 06 Jan 2010 at 10:15 am

    Thanks for a great writeup again Gojko.

    It looks like your markup of <action>, <subject> and <greeting> got eaten. (I’m gambling that mine come through when I press Submit Comment).

    Cucumber (without Cuke4Nuke) actually *does* report what the difference of two tables is, colouring surplus or missing rowns and columns. See “Diffing Tables” at http://wiki.github.com/aslakhellesoy/cucumber/multiline-step-arguments. The thing is, this isn’t working properly over the wire protocol and Cuke4Nuke yet. I expect it will be in a future version of Cuke4Nuke.

  2. gojkoon 06 Jan 2010 at 7:53 pm

    thanks for the correction. scenario fixed.

  3. Antonyon 14 Jan 2010 at 3:58 pm

    Thanks for the introduction – found it exceptionally helpful

Trackback URI | Comments RSS

Leave a Reply