Home > BDD > Easy requirements by example

Easy requirements by example

There is this fun framework called easyb http://www.easyb.org/ that could be used to make you stories or requirements by example executable in an easy way.
In the article ‘Introducing BDD’ http://dannorth.net/introducing-bdd Dan North discusses BDD and uses the following user story for explanation.

Story: Customer withdraws cash
As a customer,
I want to withdraw cash from an ATM,
so that I don’t have to wait in line at the bank..

Well, now let’s assume you are in a meeting discussing this user story and as a good team you ask the customer to give some specific examples of it. During the discussion you are of course helped by the testers who help the team also focus on ‘things the system should not do’. At the end of the meeting you have a good understanding of the user story and have a couple of written examples such as the one shown below.

scenario Account is in credit
given the account has 10 dollars in credit
and given the dispenser contains 1000 dollars
and given the card is valid
when the customer requests 5 dollars
then ensure the account is debited and now contains 5 dollars
and then ensure cash is dispensed from the dispenser and the dispenser now has 995 dollars

It would be great if you could translate this easily to an executable specification so that you could start developing your Java or Groovy code against it. The good news is that you can do this easily with easyb. You could write your test as follows:

scenario “Account is in credit”, {
given “the account has 10 dollars in credit”, {
//do some stuff here
}
and
given
“the dispenser contains 1000 dollars”, {
//do some stuff here
}
and
given
“the card is valid”, {
//do some stuff here
}
when “the customer requests 5 dollars”, {
//do some stuff here
}
then “ensure the account is debited and now contains 5 dollars”, {
//do some stuff here
}
and
then
“ensure cash is dispensed from the dispenser and the dispenser now has 995 dollars”, {
//do some stuff here
}

}

This almost the same as the text from the scenario and is easy for non technical people to read and understand! Once you have this you can start developing and could arrive at something like this:

scenario “Account is in credit”, {
given “the account has 10 dollars in credit”, {
accountInCredit = Account.create(10)
}
and
given
“the dispenser contains 1000 dollars”, {
dispenser = Dispenser.create(1000)
}
and
given
“the card is valid”, {
card = Card.create(accountInCredit)
card.enabled = true
dispenser.insertCard card
}
when “the customer requests 5 dollars”, {
dispenser.requestCash 5
}
then “ensure the account is debited and now contains 5 dollars”, {
accountInCredit.amount.shouldBe 5
}
and
then
“ensure cash is dispensed from the dispenser and the dispenser now has 995 dollars”, {
dispenser.amount.shouldBe 995
}
}

Well…. you probably would not arrive at something like this in a real system but this is just an example…
You could use this for unit testing, using the groovy build in possibilities of mocking and stubbing and if that’s not enough you could use Java frameworks like EasyMock, and DbUnit to name a few.

But I see more value for this on integration testing your system and creating a suite of acceptance test scenarios for a iteration. You can easily mail it, print it to discuss and its easy to come up with more test cases once you have a example.

Advertisements
Categories: BDD Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: