PHPUnit gives No Code Coverage Driver Is Available

6.6k Views Asked by At

I have a laravel project and I tried to run phpunit to give me a coverage report. I ran the command vendor/bin/phpunit --coverage-html storage/test-output-data/coverage-html and it gave the output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
Error:         No code coverage driver is available

I tried to resolve this situation by doing a sudo pecl install pcov, but running the phpunit still gave the same error above.

I noticed that I have the file /usr/lib/php/20170718/pcov.so. So I added the line extension=/usr/lib/php/20170718/pcov.so to my /etc/php/7.4/cli/php.ini file. But when I type php -v, I get this error:

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php/20170718/pcov.so' (tried: /usr/lib/php/20170718/pcov.so (/usr/lib/php/20170718/pcov.so: undefined symbol: _zval_ptr_dtor), /usr/lib/php/20190902//usr/lib/php/20170718/pcov.so.so (/usr/lib/php/20190902//usr/lib/php/20170718/pcov.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
PHP 7.4.11 (cli) (built: Oct 10 2020 19:44:50) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.11, Copyright (c), by Zend Technologies

I also have a folder called /usr/lib/php/20190902 but it does not have a pcov.so file.

I also tried doing a sudo pecl install xdebug, but I run into similar errors as above where phpunit says there's no driver and typing php -v says some global variable is missing similar to the pcov issue.

What am I doing wrong?

3

There are 3 best solutions below

0
On BEST ANSWER

I noticed that I have the file /usr/lib/php/20170718/pcov.so. So I added the line extension=/usr/lib/php/20170718/pcov.so to my /etc/php/7.4/cli/php.ini file.

This file is for PHP 7.2 (based on 20170718 part) while you are trying to use it in PHP 7.4.

You need to get the right version of Xdebug (or PCOV) for your PHP 7.4. (NOTE: for PHP 7.4 the API version (and the right folder in the path) will be 20190902.)

Try this solution from the following answer to switch / install extensions via PECL for specific PHP version (7.4 instead of your current 7.2): https://stackoverflow.com/a/54594604/783119

sudo pecl -d php_suffix=7.4 install <package-name>
0
On

This has nothing to do with PHPUnit. You have a problem with your PHP environment: PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20170718/pcov.so' (tried: /usr/lib/php/20170718/pcov.so (/usr/lib/php/20170718/pcov.so: undefined symbol: _zval_ptr_dtor), /usr/lib/php/20190902//usr/lib/php/20170718/pcov.so.so (/usr/lib/php/20190902//usr/lib/php/20170718/pcov.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

0
On

You just need to install xdebug, so the coverage knows how to run.

For example, for PHP 7.1, it's this package:

sudo apt install php7.1-xdebug

Just fixed it today for my scenario. In your case, just change the PHP version to:

sudo apt install php7.4-xdebug