$ cat "

Unit Testing in PHP


Recently I have begun to adopt a test driven style of development, trying to do it the Kent Beck way, i.e. test first. When I started planning the development of this site I remembered reading about two different unit testing frameworks for PHP, namely PHPUnit and Simpletest. I decided to start with PHPUnit, and to my delight it was available as an apt package in my beloved Ubuntu, so the installation went as smooth as only apt installations can.

I tried it out on the shipped example, a unit test of an array, and was a bit disapointed with the interface. I had envisioned a PHP based framework that you simply ran in your browser, providing a nice HTML GUI. Instead it was a command line utility, that gave a quite sparse feedback, when running the tests. It had some nice features as report generation in various formats and testing code coverage, so it seemed powerful, but lacking the green bar/red bar feedback.

So, Google to the rescue. I did a quick search and found the documentation for the PHPUnit API basically explaining a nice way of using the API to create a GUI wrapper for PHPUnit. Another search resulted in a source forge project called Cool ("Cool Object Orientated Library", a little GNU-esque there... and it really is "orientated"). This turned out to be just what I was looking for. It was a php site, that provided a nice GUI for PHPUnit. The installation was painless, since all you had to do was to extract so that it is available from the web server.

I started playing around with Cool and PHPUnit and, unfortunately, it soon became obvious that Cool did not handle test suites that well, or at least that it did not work properly in my set-up. Once again, to the Google-mobile, but no luck, so I decided to give Simpletest a try instead.

As with Cool, all you had to do to install Simpletest was to extract it to a place that was reachable by your PHP code and you were done. I was glad to find that it shipped with a simple HTML GUI, so this was more in line with what I first had in mind. The documentation was well written and had good examples, so it did not take long to get up and running. Since everything felt solid I decided to use Simpletest for the project.

Simpletest turned out to be really nice to work with and it has a nice Mocking-framework, that was easy to use. The only down side was the HTML reporter which was very basic, so after a while I decided to write a slightly more advanced reporter. The API used by the reporter classes was documented and since the source for the original HTML reporter was available it was quite a simple job to write a custom reporter.

So, if you want a light weight, portable testing framework for PHP, I can recommend Simpletest. I will probably post my extended HTML reporter at this site in the near future, if anyone wants to try it out.

Written by Erik Öjebo 2008-09-04 13:58