Empty or Zero Percent (0%) Code Coverage With PHPUnit and pcov

This was a frustrating issue that took a ton of googling and half a day of pulling my hair out!

I recently updated my PHP version and, since I use Docker, I upgraded the image version to get the newer version. After doing this, my PHPUnit tests stopped reporting coverage. With v8.5 of PHPUnit I saw all the files in the report but every one was returning 0% coverage. I upgraded to 9.6 in an effort to fix the issues, and the report suddenly showed no files at all. So it was crystal clear there was a separate issue!

Here's the solution (for me)

After a whole load of Googling, I finally took a look at the php configuration using the php -i | grep pcov command. Here's the result:

pcov
pcov.directory => /var/www/lib
pcov.exclude => none
pcov.initial.memory => 65336 bytes
pcov.initial.files => 64

It may not be obvious on first glance, but there's a line that is wrong. The pcov.directory is pointing to a directory that doesn't exist. If you run the above and get a similar result, then this could well be your problem.

To fix it, you just need to update the one of your php configuration files (eg php.ini, or /usr/local/etc/php/conf.d/docker-php-ext-pcov.ini for Docker) with the following:

pcov.directory=/var/www

Where you replace /var/www with the location of your code, or the default 'auto'. Full details of the pcov configuration can be found here.

If you're running in Docker, you could also run the following after your run the docker-php-ext-enable pcov command:

 
RUN echo "pcov.directory=/var/www/app" >> /usr/local/etc/php/conf.d/docker-php-ext-pcov.ini

I reran my tests and suddenly I was seeing coverage. Hooray!

Posted on Feb 14, 2024

Discuss This

blog comments powered by Disqus