Use other code coverage driver than xdebug

1.8k Views Asked by At

I want to use pcov instead of xdebug for code coverage generation.

I'm using Docker and I have xdebug installed.

Can I be sure that xdebug won't affect test execution if I run the following command?

php -d xdebug.default_enable=0 -d pcov.enabled=1 path/to/phpunit --coverage-text

I read that pcov might be faster but as I understood xdebug has to be disabled.

Is it better do the following to achieve the fastest coverage instead of running the above command?

  1. remove/truncate xdebug config
  2. run tests

    php -d pcov.enabled=1 path/to/phpunit --coverage-text
    
  3. restore xdebug config

3

There are 3 best solutions below

0
On

Is it better do the following to achieve the fastest coverage instead of running the above command? [disable/enable xdebug/pcov instead of loading them]

as you're running php on the commandline, you don't need to fiddle with ini files invoking phpunit.

instead you can make the runtime configuration explicit with command-line parameters, this offers an interesting effect often.

it works with the -n switch, that is disabling all configuration files (see php --help for usage info):

php -n [...]

where [...] stands for the arguments that are specifically for the use-case, in general and specific here for phpunit:

php -n <php-file> [<php-file-argument>...]

       `------------ [...] --------------´ 


php -n path/to/phpunit --coverage-text

       `--------- [...] -------------´

the -n switch/option makes the runtime really naked, so you start with a clean slate.

first of all running phpunit may not work at all and would not for some features (like reading the configuration file), because phpunit needs some php extensions and -n controlled to not load any php extensions (that is php only have the core extension or those that are compiled in and can not be deactivated).

Therefore you have to add them, e.g. Dom for the XML configuration file and Tokenizer for generating the HTML code-coverage report (soon):

 php -n -d extension=dom -d extension=tokenizer [...]

Then your test-suite most likely also tests paths of your code that requires extensions. invoking phpunit will highlight these in failures. Therefore you have to add them (e.g. here json):

 php -n -d extension=dom -d extension=tokenizer -d extension=json [...]

This is perhaps the interesting part, as you learn about the extension requirements your code has (at least for unit-testing).

Finally add the coverage extension of choice. Let's take pcov for the example:

php -n -d extension=dom -d extension=tokenizer -d extension=json \
    -d extension=pcov -d pcov.enabled=1 [...]

and then you get your results:

PHPUnit 9.5.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.4.20 with PCOV 1.0.8
Configuration: phpunit-cfg.xml

...............                                                   15 / 15 (100%)

Time: 00:00.191, Memory: 6.00 MB

OK (15 tests, 33 assertions)

Generating code coverage report in HTML format ... done [00:00.021]

Compare against xdebug? Why not:

hp -n -d extension=dom -d extension=tokenizer -d extension=json \
   -d zend_extension=xdebug -d xdebug.mode=coverage [...]
      ^^^^^

and have the results:

PHPUnit 9.5.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.4.20 with Xdebug 3.0.4
Configuration: phpunit-cfg.xml

...............                                                   15 / 15 (100%)

Time: 00:00.222, Memory: 8.00 MB

OK (15 tests, 33 assertions)

Generating code coverage report in HTML format ... done [00:00.024]

The hinted phpunit-cfg.xml file was one created with phpunit --generate-configuration and code-coverage enabled. The output examples have been shortened for clarity.

1
On

xdebug will cost performance even if disabled with:

xdebug.default_enable=0

The impact is not negligible.

You're better off disabling the xdebug extension completely before running your tests.

This will give you the best performance if you're using pcov to generate the code-coverage.

0
On

Xdebug and PCOV both overload the same parts of the engine, as a result they are incompatible, and there's no sense in the authors trying to make them compatible.

Xdebug must not be loaded if you want to use PCOV as the driver for php-code-coverage.

Source: I wrote pcov ...