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

robertobutti

Roberto B.

Posted on March 5, 2023

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.

💖 💪 🙅 🚩
robertobutti
Roberto B.

Posted on March 5, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related