Behavior-Driven Development (BDD) is an evolution of Test-Driven Development (TDD) which pushes the focus away from the concept of testing to one of specifying behaviours in a form more easily understood. It’s actually pretty hard to explain BDD in a few short lines but I’ll throw in a quick intro after posing a question:

Are there any usable PHP Behavior-Driven Development libraries? Anywhere? I get the feeling PHP has once more fallen behind Ruby and Java which have four such libraries between them. Anyway – if not I’ll have to cheat with SimpleTest for now.

Anyway, in the past I undertook to learn Ruby and got started with the RSpec BDD library after a few weeks – thanks to Brian D. for the introduction to RSpec. If you prefer a Java outlook, there’s JBehave or JDave. The reason for having a replacement for the traditional Unit Testing tools is obvious after you’ve practiced BDD for a while. Unit Testing tools don’t support the BDD terminology and ways of thinking – it’s possible to cheat with JUnit (and PHPUnit/SimpleTest in my case) using test messages and careful method naming but the result isn’t very pretty even if its functional, and you lose a lot of the benefits by having to put up with the “test” oriented nature of an xUnit lib and it’s lack of a fluid language base.

Here’s a typical RSpec behaviour spec of a non-existent Fleet class for a game. Just as with TDD, we write the specification (not “test”!) first and then write enough code to meet it.

[geshi lang=ruby]describe Fleet, “when first instantiated” do

before(:each) do
# just like using setUp() in PHP UT
@fleet = Fleet.new
end

it “should contain no Ships after formation” do
@fleet.population.should eql(0)
end

after(:each) do
@fleet = nil
end

end[/geshi]
BDD avoids any mention of “test”; since it revolves around specifications. I know that sounds a bit vague – just trust me that BDD does not involve Unit Tests in the classical sense. RSpec notes itself as providing a “Domain Specific Language with which you can express executable examples of the expected behaviour of a system”. See, not tests and no mention of units either ;) .

This tiny example can be summed up by followed the keywords:

Describe a Fleet when first instantiated:
– it should contain 0 Ships after formation
– …

This small text snippet “describes” a Behaviour by setting out what “it should” do. In fact a good BDD library will generate such text summaries for you. Honestly, after a few months of ocassional RSpec usage this has become a pretty intuitive way to think of things – more so than TDD which requires you hit “behaviour” keyword on your own initiative.

It get’s cooler when you create a Squadron which needs to behave like a Fleet. RSpec gets around this by introducing the concept of “shared behaviors” which I will not describe here except to say it’s a really neat approach involving Refactoring.

Any other BDD fans in the audience? Any last ditch memory of a PHP BDD library? ;)

Related posts:

  1. Eclipse PDT (PHP Development Tools) 1.0 Released Today
  2. Thoughts on a Unit Testing and Test-Driven Design Experience
  3. blogBB and Test-Driven-Development
  4. Prodigal PHP Developer Returns
  5. Refactoring an OpenID Library