Testing code coverage (have I tested everything?) in PHP
Archive - Originally posted on "The Horse's Mouth" - 2012-12-21 13:57:53 - Graham EllisWhen you write a series of tests for your code and they all pass, have you tested everything? Not Necessarily - you only know that you have passed all the tests that you set, and not that the test set could be considered to be complete.
PHPUnit - the unit test suite for PHP - includes a coverage option which will let you check whether each class and method of the code that you're testing has been run as a part of the tests, and will tell you how many times each statement has been executed during testing. Simply run the phpunit command with the appropriate option:
phpunit --coverage-html /var/www/html/cover five.php
As the results are extensive, they don't come as a single file but rather as a complete web folder, and in my command I've redirected that to a directory that's within my web document root so that I can browse to it:

And I can also explore the source code and the number of times each conditional group of statements / loop has been run:

For details of writing the tests for PHPunit, see previous article [here].