Skip to content

Instantly share code, notes, and snippets.

@tertek
Last active October 30, 2025 09:49
Show Gist options
  • Save tertek/bd25073983870873363c0d4f64f6cf0e to your computer and use it in GitHub Desktop.
Save tertek/bd25073983870873363c0d4f64f6cf0e to your computer and use it in GitHub Desktop.
PHPUnit Code-Coverage with PCOV on Windows (Local Development Environment)

PHPUnit Code-Coverage with PCOV on Windows

Important

This gist is not updated. Please read the comments below.

Download and Install PCOV

  1. Download latest release (currently 1.0.11) for Windows for your version of PHP and the correct format (ts or nts) from PECL
  2. Unzip downloaded package and copy the files php_pcov.dll and php_pcov.pdb to your PHP extensions directory, e.g. C:\bin\php\php-7.4.27-Win32-vc15-x64\ext\
  3. Modify your php.ini so that PCOV is loaded. Therefore add extension=pcov:
..
extension=fileinfo
extension=gd2
extension=pcov
..
  1. Reload your Webserver, e.g. Apache or Nginx

Configure PHPUnit

Create a PHPUnit configuration file with the wizard by running:

 .\vendor\bin\phpunit --generate-configuration

Then open the file phpunit.xml and add directories to be excluded, otherwise the command will output too many errors:

<?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="vendor/autoload.php"
         cacheResultFile=".phpunit.cache/test-results"
         executionOrder="depends,defects"
         forceCoversAnnotation="true"
         beStrictAboutCoversAnnotation="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         convertDeprecationsToExceptions="true"
         failOnRisky="true"
         failOnWarning="true"
         verbose="true">
    <testsuites>
        <testsuite name="default">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <coverage cacheDirectory=".phpunit.cache/code-coverage"
              processUncoveredFiles="true">
        <include>
            <directory suffix=".php">.</directory>
        </include>
        <exclude>
            <directory>./vendor</directory>
            <directory>./tests</directory>
        </exclude>
    </coverage>
</phpunit>

Install PHPUnit Code-Coverage Package

Add PHPUnit Code Coverage package by requiring with Composer:

composer require --dev phpunit/php-code-coverage

Use phpunit with code coverage

.\vendor\bin\phpunit tests --coverage-html ./html
@matbind
Copy link

matbind commented Oct 22, 2025

Hi! Since this is one of the top google results when trying to find information about how to install PCOV on Windows, I think it'd be worth updating the link to PCOV 1.0.12 https://pecl.php.net/package/pcov/1.0.12/windows

Also, the phpunit.xml syntax is different since PHPUnit 10. Here is an example snippet

<coverage>
    <report>
        <text outputFile="php://stdout"/>
        <html outputDirectory="tests/coverageReports"/>
    </report>
</coverage>

<source>
    <include>
        <directory suffix=".php">app</directory>
    </include>
    <exclude>
        <directory>vendor</directory>
        <directory>tests</directory>
    </exclude>
</source>

Thanks for the writeup :)

@tertek
Copy link
Author

tertek commented Oct 22, 2025

@matbind Thank you for you additions. Since I am not working with Windows anymore and probably won't ever again, I suggest you can fork the gist and we reference to yours?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment