2

I use phpUnit with PCOV on code coverage on window. But somehow, I get 0.0% code coverage. I am sure the function is called. Why is this happening?

Versions:

  • PHP: 8.1.12
  • PHPUnit: 9.5.0
  • PCOV: 1.0.11

The running log is as follows

PS C:\xampp\php> vendor\bin\phpunit
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.12
Configuration: C:\xampp\php\phpunit.xml

R                                        1 / 1 (100%)Called1
Time: 00:00.215, Memory: 10.00 MB
There was 1 risky test:

1) AAATest::testaccess
This test printed output: Called1
OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 1, Risky: 1.
PS C:\xampp\php\

Following is my simplified code AAA.php

class AAA
{
    public $flag;
    public function __construct($flag)
    {
        $this->flag = $flag;
    }

    public function access()
    {
        $this->flag = 1;
    }
}

Following is my testing AAATest.php

<?php

require '/xampp/php/vendor/autoload.php';
require '/xampp/htdocs/Performance/scripts-php/AAA.php';

use PHPUnit\Framework\TestCase;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Driver\Driver;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlReport;

$filter = new Filter;
$filter->includeFile('/xampp/htdocs/Performance/scripts-php/AAA.php');
$driver = Driver::forLineCoverage($filter);
$coverage = new CodeCoverage($driver, $filter);

$flag = 0;

class AAATest extends TestCase
{
    protected $AAA;

    protected function setUp(): void
    {
        global $flag;
        global $coverage;
        $this->AAA = new AAA($flag);
        print("Called");
    }

    /**
     * @covers AAA::access
    */
    public function testaccess()
    {
        global $flag;
        global $coverage;

        $coverage->start('testaccess');

        $this->AAA->access();

        print($this->AAA->flag);

        $this->assertEquals(1, $this->AAA->flag);

        $coverage->stop();

        (new HtmlReport)->process($coverage, 'code-coverage-report\report');
    }
}

Following is the php.ini

........
extension=php_gd
extension=php_pcov
extension=gettext
.......

Following is the phpunit.xml

<?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">
            <file>/xampp/htdocs/Performance/scripts-php/AAA.php</file>
            <directory>/xampp/htdocs/Performance/scripts-php/</directory>
        </testsuite>
    </testsuites>


    <coverage cacheDirectory=".phpunit.cache/code-coverage"
            processUncoveredFiles="true"
    >
        <include>
            <directory suffix=".php">/xampp/htdocs/Performance/scripts-php</directory>
        </include>
        <exclude>
            <directory>./vendor</directory>
            <directory>./tests</directory>
        </exclude>
    </coverage>
    <php>
        <env name="APP_ENV" value="testing"/>
    </php>

</phpunit>

Following is the composer.json

{
    "require-dev": {
        "phpunit/phpunit": "9.5",
        "phpunit/php-code-coverage": "^9.2",
        "phpunit/phpunit-selenium": "^9.0",
        "pcov/clobber": "^2.0"
    },
    "require": {
        "php": ">=8.0",
        "php-webdriver/webdriver": "1.13.1",
        "symfony/symfony": "5.2"
    }
}

Here is the result. I am expecting 100% code coverage

C:\xampp\php\code-coverage-report\report\AAA.php.html
    
                       Code Coverage          
       Lines                        Functions and Methods               Classes and Traits
Total 
  0.00% covered(danger)    0.00% 0/2 0.00% covered(danger)    0.00% 0/2 CRAP0.00% covered (danger)  0.00%  0/1
AAA 
  0.00% covered (danger)   0.00% 0/2 0.00% covered(danger)    0.00% 0/26 0.00% covered (danger)
0.00% 0/1
 __construct    
  0.00% covered(danger)    0.00% 0/1 0.00% covered(danger)    0.00% 0/1 2   
access  
  0.00% covered (danger)   0.00% 0/1 0.00% covered(danger)    0.00% 0/1 2   
0

1 Answer 1

0

Try adding the source directory (and the excludes) to the pcov configuration in your php.ini as well:

pcov.directory = "/xampp/htdocs/Performance/scripts-php"
pcov.exclude   = "#/(vendor|tests)/#"

See this related issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.