Sunday, March 13, 2011

An introduction to BDD using the StoryQ framework

BDD is a higher level of unit testing by using terminology focused on the behavioral aspects of the system rather than testing, it creates better documentation of your system by recording its intent which makes your system easier to learn for new developers and relearn for when you revisit your code further down the line. BDD is a practice that has evolved out of established agile practices and is designed to make them more accessible and effective for teams new to agile software delivery. Over time, BDD has grown to encompass the wider picture of agile analysis and automated acceptance testing.
BDD lets you to write test in the concept of Given-when-then style. For e.g. a normal BDD test case looks like.

new Story("Items can have unit price")
    .InOrderTo("Supermarket can have items with unit prices")
    .IWant("To idenify items with unit prices in the supermarket and get the prices from the checkout list")
    .WithScenario("Items in supermarket will have unit prices")

This is as simple as converting a user story defined in a template like
·         As a (X)
·         I want to (Y)
·         So that (Z) where Y is some feature, Z is the benefit or value of the feature, and X is the person (or role) that will benefit.
·         Given (X)
·         When (Y)
·         Then (Z) where X is some given context, Y is the event or action occurred in the context and Z ensures some outcome.
Frameworks for BDD?
In this post we’ll see how to do BDD using the StoryQ framework. StoryQ  is a portable (single dll), embedded BDD framework for .NET. It runs within your existing test runner and helps produce human-friendly test output (html or text). StoryQ's fluent interface adds strong typing, intellisense and documentation to your BDD grammar. We’ll convert our sample for supermarket pricing coding kata to BDD test cases using the StoryQ framework.
The tests to check whether items have a name and unique number is written as

public void ItemTestMethod()
    new Story("Items in the supermarket should have a name")
        .InOrderTo("Identity items at checkout")
        .IWant("To identify the items in my checkoutlist")
        .WithScenario("Adding items")

public void ItemNumberTest()
    new Story("Items in the supermarket should have a unique number that is system generated")
    .InOrderTo("Uniquely identify items in the supermarket")
    .IWant("Uniquely identify items in the supermarket")
    .WithScenario("Adding items")

Then the output of the testrunner will be a report generated as

No comments: