PHP: PHPUnit Coverage

Xampp

Setup XDebug here.

$env:xdebug_mode="coverage"
php artisan test --coverage

Docker

pecl install xdebug
docker-php-ext-enable xdebug
php --ini
xdebug.mode=coverage
XDEBUG_MODE=coverage vendor/bin/phpunit

Composer

composer init
composer require --dev phpunit/phpunit
vendor/bin/phpunit --generate-configuration
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         cacheDirectory=".phpunit.cache"
         executionOrder="depends,defects"
         requireCoverageMetadata="true"
         beStrictAboutCoverageMetadata="true"
         beStrictAboutOutputDuringTests="true"
         failOnRisky="true"
         failOnWarning="true">
    <testsuites>
        <testsuite name="default">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <coverage>
        <include>
            <directory suffix=".php">src</directory>
        </include>
    </coverage>
</phpunit>
<?php

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
...

#[CoversClass(SomeClass::class)]
final class SomeClassTest extends TestCase {
    ...
}
<?php

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
...

/**
 * @covers \SomeClass
 */
final class SomeClassTest extends TestCase {
    ...
}

References:


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *