How to have code coverage report with pcov?

2.8k Views Asked by At

I use PHP 8.0.5 , PHPUnit 9.5.4, PHPStorm 2021.1.2.
When, in my XML configuration file, I use this :

<coverage pathCoverage="true">
  <report>
    <clover outputFile="./coverage.xml"/>
  </report>
</coverage>

(This is only the relevant portion of my file)

...and launch the command 'Run with coverage' from PHPStorm, it does not generate the coverage report.

I have deactivated XDebug. PHPStorm shows me :

Warning: XDEBUG_MODE=coverage or xdebug.mode=coverage has to be set

I do not know why it mentions XDebug as I want to use pcov as I set it in the "Run/Debug Configurations" popin. I try to put the report in another folder, try to generate HTML report instead... launch the command via the terminal ... None of this works. I did not find anything on Google about my issue.

EDIT

I tried with XDebug not loaded at all and this simplified configuration (created with the --generate-configuration parameter) and added the previous code (without the path coverage).

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
         bootstrap="TestBootstrap.php"
         cacheResultFile=".phpunit.cache/test-results"
         executionOrder="depends,defects"
         forceCoversAnnotation="true"
         beStrictAboutCoversAnnotation="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         failOnRisky="true"
         failOnWarning="true"
         verbose="true">
    <testsuites>
        <testsuite name="default">
            <directory suffix="Test.php">./src/BlocksTest.php</directory>
        </testsuite>
    </testsuites>

    <coverage cacheDirectory=".phpunit.cache/code-coverage"
              processUncoveredFiles="true">
        <include>
            <directory suffix=".php">../src</directory>
        </include>
        <report>
            <clover outputFile="./coverage.xml"/>
        </report>
    </coverage>
</phpunit>

It still not works even if the library says :

Generating code coverage report in Clover XML format ...

1

There are 1 best solutions below

3
On

There are certainly many ways to answer the question, while we're finding out, I leave some first drafts.


Your Phpunit XML configuration:

<coverage pathCoverage="true">
  <report>
    <clover outputFile="./coverage.xml"/>
  </report>
</coverage>

is with the pathCoverage attributes true value.

[In Phpunit 9.5] The pathCoverage Attribute [...] When set to true [..] This requires a code coverage driver that supports path coverage. Path Coverage is currently only implemented by Xdebug.

So you can not have this code coverage report with pcov.

Your Phpunit configuration is incompatible.


When Phpunit (via sebastianbergman/php-code-coverage) shows this warning:

Warning: XDEBUG_MODE=coverage or xdebug.mode=coverage has to be set

It means that it detected the xdebug extension is available but it is not configured in a way to use it, therefore hints which parameters to change.

If you're on the command-line (CLI) replaying the last command with the xdebug mode environment parameter is often most easy for a test-run:

$ XDEBUG_MODE=coverage !! # press enter

otherwise any of the two options works with PHP, configure the run job appropriately.

However if you are not concerned about xdebug but pcov

I do not know why it mentions XDebug as I want to use pcov as I set it [in PhpStorm] in the "Run/Debug Configurations" popin .

The warning about the xdebug configuration tells a different story: xdebug and pcov are exclusive and

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

Which means - as the original warning shows that Xdebug is loaded - it has to be unloaded first.

PhpStorm can not unload extensions for the configured PHP version. What it does it that it adds extensions and configuration settings based on your choice.

In PhpStorm similar as it is on the command line (CLI) you can control both environment parameters as well as command line arguments for the PHP executable.

If the existing configuration of the php executable on the system loads already too much, as a last resort you can then disable all extensions first with the -n switch and add extensions as needed.

Some extensions like xdebug need to be loaded as a zend extension:

For a description of the PHP command line options, please see Command line options[DOCS].

For a description of the diverse configuration options, please see either:

  • the commented php.ini file(s) that ship with your PHP distribution
  • the appropriate page in the PHP manual (it also has a List of php.ini directives[DOCS])
  • additional documentation by extension authors, e.g. the Xdebug Homepage often has detailed configuration instructions and covers all ini settings.