Tag: PHPUnit

PHPUnit: faster and better unit tests with pcov

When using PHPUnit there are different ways to create a code coverage report. By default, XDebug is used. But as mention on different sites, XDebug is very slow and the generation of a code coverage report might take several minutes for big projects.

phpdbg

To speed up things, phpdbg can be used. This significantly speeds up unit tests and code coverage. Phpdbg can be used as follows:

phpdbg -qrr ./vendor/bin/phpunit --coverage-text --colors=never

But there are different problems with the code coverage report of phpdbg. For example phpdbg does not cover a case line of a switch statement:

pcov

A better solution to cover this is pcov, which can be installed using pecl:

pecl install pcov

To create a code coverage report, phpunit can be called with:

php -dpcov.enabled=1 -dpcov.directory=. ./vendor/bin/phpunit --coverage-text

To exclude a directory, the following parameter can be used:

php -dpcov.enabled=1 -dpcov.directory=.  -dpcov.exclude="~vendor~" ./vendor/bin/phpunit --coverage-text

I don’t know if it’s really a problem with pcov, but: with pcov installed, it is not possible to use phpdbg anymore!