Search code examples
phplaravelphpunitlaravel-testing

Laravel not generating code coverage report


I am using Laravel 8 and to test my app, I am running

php artisan test --coverage-html reports

The tests are running successfully. The problem is that there is no coverage reports generated. I have gone through answers here on SO and solution is said to add the below to my phpunit.xml

 <logging>
    <log type="coverage-html" target="tests/coverage" showUncoveredFiles="true"/>
    <log type="coverage-clover" target="build/logs/clover.xml"/>
  </logging>

That doesn't work. I also tried

    <coverage processUncoveredFiles="true">
      <include>
        <directory suffix=".php">./app</directory>
      </include>
      <report>
        <html outputDirectory="tests/reports/coverage"/>
      </report>
    </coverage>

Still nothing; Below is my phpunit.xml file

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
  <testsuites>
    <testsuite name="Unit">
      <directory suffix="Test.php">./tests/Unit</directory>
      <directory suffix="Test.php">./packages/okotieno</directory>
    </testsuite>

    <testsuite name="Feature">
      <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>
  </testsuites>
  <filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
      <directory suffix=".php">./app</directory>
    </whitelist>
  </filter>
  <logging>
    <log type="coverage-html" target="tests/coverage" showUncoveredFiles="true"/>
    <log type="coverage-clover" target="build/logs/clover.xml"/>
  </logging>
  <php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="MAIL_DRIVER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <env name="DB_DATABASE" value="testing_db"/>
  </php>

</phpunit>

I also ran php artisan test --help to see the valid flags, --coverage-html is not in the list but it runs. How can I fix this?


Solution

  • Make sure xdebug is installed and configured in php ini.

    If installed php -v will show the below results with xdebug details

    PHP 8.0.15 (cli) (built: Jan 21 2022 04:49:41) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v4.0.15, Copyright (c) Zend Technologies
       with Xdebug v3.1.3, Copyright (c) 2002-2022, by Derick Rethans
       with Zend OPcache v8.0.15, Copyright (c), by Zend Technologies
    

    make sure xdebug coverage mode is enabled in the php ini file

    [debug]
    zend_extension=/usr/local/Cellar/[email protected]/8.0.15/pecl/20200930/xdebug.so
    xdebug.mode=coverage,debug
    xdebug.start_with_request=yes
    xdebug.client_port=9003
    xdebug.log_level=7
    xdebug.idekey=VSCODE
    xdebug.client_host=127.0.0.1
    

    Add without in phpunint.xml file as direct child of

    <coverage processUncoveredFiles="true">
       <include>
         <directory suffix=".php">./app</directory>
       </include>
    </coverage>
    

    Run test with artisan passing path for code coverage report

    php artisan test --coverage-html tests/reports/coverage