DEV Community

Cover image for Test coverage: did you set Xdebug's coverage mode?
Roberto B.
Roberto B.

Posted on

Test coverage: did you set Xdebug's coverage mode?

If you are using PestPHP and during the execution of your tests with the coverage option, you are receiving the message:

Unable to get coverage using Xdebug. Did you set Xdebug's coverage mode?
Enter fullscreen mode Exit fullscreen mode

you should check one of these three things:

  • checking the Xdebug configuration;
  • setting the XDEBUG_MODE environment variable;
  • adjusting the PHPUnit configuration file.

Check the Xdebug configuration

My first suggestion is to check your Xdebug configuration in the php.ini file:

[xdebug]
zend_extension="xdebug.so"
xdebug.mode=develop,debug,coverage
xdebug.start_with_request = yes
Enter fullscreen mode Exit fullscreen mode

With the xdebug.mode parameter, you must check the presence of the coverage value.

Set the XDEBUG_MODE parameter

An alternative way to instruct Xdebug to use the coverage mode is using the XDEBUG_MODE environment variable.
You can set the environment variable and launch tests in the one command line:

XDEBUG_MODE=coverage ./vendor/bin/pest --coverage
Enter fullscreen mode Exit fullscreen mode

Set the coverage section in the phpunit.xml file

If the previous two solutions (which are very similar) didn't work, you could check the PHPUnit configuration.
PestPHP, under the hood, uses the great tool PHPUnit. Yes, as PHP developers, we are lucky to have the best open-source tools for software development.
So the PHPUnit configuration reflects and influences the PestPHP execution.
For coverage operations, in the PHPUnit configuration, you should use the coverage section.
In the coverage section, make sure that you are including the right directories (where your source code is located). So, for example, if you have your source code in src/ directory, check if you have the coverage section with the proper include in the phpunit.xml or phpunit.xml.dist file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
         backupGlobals="false"
         backupStaticAttributes="false"
         colors="true"
         verbose="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">

    ... other configuration settings ...

    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./src</directory>
        </include>
    </coverage>
</phpunit>
Enter fullscreen mode Exit fullscreen mode

If you had the "Unable to get coverage using Xdebug" issue, let me know if these solutions work for you.

Top comments (2)

Collapse
 
dcblog profile image
David Carr

Excellent guide, thanks!

Collapse
 
jcubic profile image
Jakub T. Jankiewicz

got this errors:

1) Test results may not be as expected because the XML configuration file did not pass validation:

  Line 9:
  - Element 'coverage', attribute 'processUncoveredFiles': The attribute 'processUncoveredFiles' is not allowed.

  Line 10:
  - Element 'include': This element is not expected.
Enter fullscreen mode Exit fullscreen mode