PHP, Zend Framework and Other Crazy Stuff
Archive for November 3, 2007
PHPSpec hits Subversion Revision 100
Nov 3rd
What is it? PHPSpec is a Behaviour-Driven Development framework for PHP currently entering it’s fourth week of consuming my free time
.
What is Behaviour-Driven Development? BDD is Test-Driven Development without all the crap that nobody really understands and everyone tends to get wrong anyway, including something called a domain specific language (DSL) that means specs are incredibly readable and clear.
Why is this better? Because it’s easy to get into, requires less experience building, and dumps the traditional Unit Testing API in favour of a domain specific language inspired by Plain English (TM).
Everyone’s favourite question is going to be how BDD differs from TDD, which is entirely valid but a question which usually leads to the short insufficient answer or the long inevitably boring answer. So I’ll try to put it in perspective insufficiently and refer to a much better explanation later in the near future.
Essentially, TDD is performed using a Unit Testing framework. Whether it’s SimpleTest, PHPUnit, Test::Unit or JUnit, it doesn’t really matter since they all share the same common approach. You call everything a Test, and within your tests you assert things. In TDD we advocate the idea of testing before coding – since it leads to better code. But TDD tends to be obsessed with Testing unless explained very very clearly. And because it’s so closely linked to testing, programmers understandably get TDD confused with Testing and think they are facets of the same thing. In fact it’s common to assume TDD is a testing practice, when in reality tests are just the byproduct of a design practice.
BDD takes a slightly different tack. It cheats. Instead of teaching you all about Unit Testing, it just jettisons all that baggage and drops you into the practice of “describing behaviour with specifications”. You do not start with a test for BDD, you start by specifying the behaviour of the system (classes/methods) you intend writing. If that sounds horribly familiar (it should to any experienced hardened practitioner of TDD) it’s because this really is what TDD is supposed to be about – if you can find it in the small print on the last page of all those TDD tutorials or the artfully concealed manual page of your UT documentation.
So with BDD, you identify a system, write specifications for it, and then implement code to the required specifications. If your code coverage sucks, well, what the hell, you’re not testing so unless you have a code coverage of <70% you're probably safe. Why? Because refactoring will naturally generate lots of extra helper classes which do not alter the specified behaviour - and if the specs have not shifted then there's little need to write new specs (more usually worthless effort) for those classes. This raises another symptom of over enthusiastic testers - tests are bound to the structure of classes/functions, not the facets of observable system behaviour. This leads to the totally uncool situation where tests are "brittle" - they change as regularly as your implementing code does. This is different to the brittleness introduced by using Mock Objects, at least Mock Objects do something useful to make their brittleness a worthwhile cost.
There's my insufficient debatable answer in all its meagre glory. To learn more about why PHPSpec is the next best thing since Travis Swicegood's new and glorious PHPT framework (which is introducing it's own optional BDD-Lite syntax, if you read his blog) visit the mailing list, or subversion repository at http://www.phpspec.org. There’s even a rumour of documentation turning up soon.
