Thursday, June 9, 2011

Behavior Driven Development as It Is

It is similar to the TDD, but the idea is to compose a set of unit tests
based on some feature (scenario), described with slightly formatted
plain English text (Gherkin language). I.e:



TDD

BDD

Just a several Unit Test + Red; Green; Refactoring stuff

One or several scenarios, described with Gherkin. Each scenario is handled to be transformed to the Unit Tests set.
This is an Agile tool, so the point is: scenario is composed during a meeting with a Customer (a person who knows how the peace of SW should behave, who might not have knowledge of UML or of a programming language) and a Programmer.
So it looks like the following:
(Gherkin)
Scenario: Printing the Data Provider input
Given that we have an input from Data Provider
When the input string is not empty
And the length of the text is more than 3 alphanumeric character long
Then the result should be put to the result string
 |<= generated with some BDD tool, e.g. SpecFlow
\|/
(Set of Unit tests, based on the Scenario)

[Given(@"that we have an input from Data Provider")]
public void GivenThatWeHaveAnInputFromDataProvider()
{
ScenarioContext.Current.Pending();// Just a default stub, should be replaced
//with Unit test functionality
}
[Then(@"the result should be put to the result string")]
publicvoidThenTheResultShouldBePutToTheResultString()
{
ScenarioContext.Current.Pending();
}
[When(@"the input string is not empty")]
public void WhenTheInputStringIsNotEmpty()
{
ScenarioContext.Current.Pending();
}
[When(@"the length of the text is more than 3 alphanumeric character long")]
public void WhenTheLengthOfTheTextIsMoreThan3AlphanumericCharacterLong()
{
ScenarioContext.Current.Pending();
}


The following Red; Green; Refactoring stuff is quite like TDD.

BDD tools permit mixed UI and Unit testing implementation. Step by step example you can find here.