DEV Community

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

Posted on

19 1

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.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (3)

Collapse
 
perisicnikola37 profile image
Nikola Perišić

Thank you. This helped :)

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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay