Unit Tests and Code Coverage with PHPT

My initial experiences with unit testing and PHP were with PHPUnit. While it’s a great tool and I have to give kudos to Sebastian for contributing so much to its development, I’ve come to appreciate the simplicity of PHPT tests. Recently, I wrote some for a project and realized that I wasn’t aware of how to generate code coverage reports. Many thanks to the very helpful patrons in the #pear channel on EFNet for helping me to get this working.

First, the stock Kubuntu installation of PEAR is version 1.6.1. Their first recommendation was to upgrade to 1.7.2, which was easy enough:

sudo apt-get install php-pearpear update-channelspear upgrade-all

Next, PEAR bug 13958 can prevent code coverage reports from being generated properly. This bug has an available patch, which you can apply by doing the following:

cd /usr/local/share/PEARwget -c http://pear.php.net/bugs/...patch PEAR/RunTest.php phpt_coverage_bug.patch.txt

Now to install PHPUnit. I ran into an odd issue here where following the installation documentation resulted in PHPUnit 1.3.3 stable being installed, which is obviously a fairly old version. Even pear clear-cache didn’t seem to solve the issue. I checked to confirm that PEAR was using PHP 5. The only way I was able to get around this was to explicitly specify the latest version.

sudo pear install phpunit/phpunit-3.2.19

The current version of PHPUnit, 3.2.19, also has bugs 482, 483, and 484 related to generating PHPT code coverage reports. I had to apply the patch for bug 482 to fix issues I was having, but the others may also be necessary depending on the code being tested. These issues should be fixed when PHPUnit 3.2.20 is released. In the meantime, if you want to avoid manually applying patches and you’re feeling adventurous, you can use an git checkout.

As far as I can tell, PHPUnit itself does not contain a runner for its PHPT suite extension. However, there is a runner script to do this in PEAR CVS. Download the script to the directory containing your PHPT test files and execute it with a command similar to this.

phpunit --coverage-html ../Tests-Results \    Console_CommandLine_AllTests AllTests.php

In the case of this particular command, the Tests-Results directory parallel to the directory containing your PHPT files should now contain an index.html file with your test results. There you have it. Happy testing folks!


  1. Thanks for the tips

  2. FYI: I talked with Sebastian about that this week. The code coverage report generator from PHPUnit is going to be refactored into a separate package so we can share it amongst all of the testing tools. No timeline on it though…

  3. …title=”View all posts in Development” rel=”category tag”>Development Matthew Turland has posted about some of his experience with PHPT tests with

  4. …p benchmark) i should be coding :: unit tests and code coverage with phpt
    My initial experiences with unit testing and PHP were with PHPUnit. While it’s a great tool a…