Introduction to PHPUnit

PHPUnit is developed by Sebastian Bergmann and is one of a small number of PHP unit testing frameworks available at the time of writing. You might prefer PHPUnit to some of its competitors because it is freely available, easy to use, and, best of all, is available from PEAR.

Perhaps rather confusingly, two packages are readily available for PHP, both of which are named PHPUnit and both of which perform essentially the same task; indeed, they even share a similar implementation. The implementation examined in this chapter is that by Sebastian Bergmann, which is better suited for PHP5.

The PHPUnit package is effectively a suite of classes rather than a single class. This chapter focuses on its Test Case and Test Suite functionality. There is more to PHPUnit, of course. If you're curious, when you're done with this chapter you may want to take a look at, which goes into more detail.

Those of you with a Java persuasion may have already drawn comparisons to JUnit (, and you would be right to do so; PHPUnit is in fact loosely based on JUnit. Much of this chapter's content may therefore already be familiar to you. What you may not be familiar with is just how easily this powerful methodology can be put to use in PHP 5.

Installing PHPUnit

A good starting point is to get your PHP setup equipped with PHPUnit. It is not part of the PEAR Foundation Classes, so you will need to install it yourself. Even if you have previously installed it, it might not be a bad idea now to check on whether you are running the latest version.

Use the standard PEAR syntax to execute an installation of PHPUnit. If you are running the latest version, PEAR will tell you. If not, or you don't have it installed, you will see output similar to the following:

   root@genesis:~# pear install PHPUnit    downloading PHPUnit-0.6.2    ...done: 11,551 bytes    install ok: PHPUnit 0.6.2    root@genesis:~# 

If you want (or need) to install manually, for example, on a production environment in which you are not blessed with root access to the server, simply visit the PEAR package page at and download and decompress the archive into an appropriate place. After everything's installed okay, you can continue safely.

Professional PHP5 (Programmer to Programmer Series)
Professional PHP5 (Programmer to Programmer Series)
Year: 2003
Pages: 182
BUY ON AMAZON © 2008-2017.
If you may any questions please contact us: