Tuesday, July 25, 2006

Introduction to BDD (Behaviour Driven Development)

BDD is an evolutino of TDD. Those that are doing TDD correctly are aware they are writing in code small specification documents. The code is then written to meet the specification, then the code is checked automatically against the specification using a tool that understands both documents. To call these specifications tests is backwards, as there is nothing to test at the point they are written. More importantly as tests they have an implied scope (unit tests test methods on classes one at a time). If you write specifications instead, then a different scope seem far more appropriate (eg. In context X these things should be true.)

When I first heard about this concept the implementations of frameworks were nothing more than Nunit and Junit with the names changed. Looking at rSpec however things have come along well. Yet again Ruby lends itself well to creating a DSL. In this case for code specification.

Here is an example from rSpec's "RSpec on Rails".

context "The Person model" do
fixtures :people, :animals

setup do
# fixtures are setup before this

specify "should find an existing person" do
person = Person.find(1)

person.should_equal people(:lachie)
person.name.should_equal 'Lachie'

specify "should have animals" do

teardown do
# fixtures are torn down after this

I love it!

Check out Dave Astel's great introduction to BDD.

No comments:

GitHub Projects