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")
    .AsA("User")
    .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")
    .Given(AnItemIsCreated)
    .And(UnitPricingStrategyIsAssignedForTheItem)
    .When(GetPriceMethodIsCalledOnTheItem)
    .Then(ShouldReturnTheUnitPriceForTheItem)
    .ExecuteWithReport(MethodBase.GetCurrentMethod());

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.
To
·         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

[TestMethod]
public void ItemTestMethod()
{
    new Story("Items in the supermarket should have a name")
        .InOrderTo("Identity items at checkout")
        .AsA("User")
        .IWant("To identify the items in my checkoutlist")
        .WithScenario("Adding items")
        .Given(ItemHasANameDefined)
        .When(GetNameIsCalled)
        .Then(ShouldReturnTheNameofTheItem)
        .ExecuteWithReport(MethodBase.GetCurrentMethod());
}

[TestMethod]
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")
    .AsA("User")
    .IWant("Uniquely identify items in the supermarket")
    .WithScenario("Adding items")
    .Given(ItemIsCreated)
    .When(GetItemNumberIsCalled)
    .Then(ShouldReturnANumberThatIsAutogenerated)
    .ExecuteWithReport(MethodBase.GetCurrentMethod());
}

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


No comments: