I’ve taken my first plunge into TDD today. Yesterday I read (skimmed) the book Test Driven Development by Example by Kent Beck. In the second section of the book Kent gave a step by step example of how to develop the xUnit testing framework in python. At the end of the section he recommended implementing your own version of xUnit for whatever language you will be testing so you can learn the methodology.
I considered using some of the existing PHP testing frameworks – but they were all rather large (>100k) and both included a lot of features I couldn’t see myself using. So I forged ahead with Kent’s advice and wrote my own testing framework, which I named “Testing”. The final results are:
testing.php – 2440 bytes – the Testing class
tests.php – 2508 bytes – the tests for testing the Testing class (say that 5x fast!)
run.php – 161 bytes – a couple lines of php to run the Testing tests
It was a challenging task, as I’ve not done TDD before, and I found it took me about 3-4 tries to actually “get it”. The first few times I just scrabbled at not knowing where to start. The 3rd time I ended up implementing the Testing class “the old fashioned way” – but then realized I was supposed to be using TDD – so I reimplemented it using TDD. The interesting bit is that by reimplementing it using TDD I was forced to make my testing framework testable – so I had to add in logging facilities early on in the process. I also uncovered several bugs while re-writing it.
All in all, it seems like TDD will be a useful tool in the project I’m working on – will keep you posted.