8

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?

Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
  • 2
    Sorry to now be able to give you a clear answer, but I am not sure if you need `xdebug` or not to do so. I also had problems a few years back when trying to generate coverage reports, so I ended up using `xDebug` coverage (with PHPStorm). – matiaslauriti Mar 30 '21 at 21:21

3 Answers3

6

I had the same problem. It was caused by the configuration of xdebug in the php.ini file. In my case, I have the xdebug 3.0.4. You can check it with the php -v.

You have to include the next lines in the php.ini file:

[XDebug]
zend_extension = "c:\xampp8\php\ext\php_xdebug-3.0.4-7.4-vc15-x86_64.dll"
xdebug.mode=debug,coverage
xdebug.client_host=127.0.0.1
xdebug.client_port=9000
user16254389
  • 61
  • 1
  • 2
5

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/php@8.0/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
8ctopus
  • 2,617
  • 2
  • 18
  • 25
Manoj
  • 66
  • 1
  • 2
  • Even though xdebug is installed & configured in php.ini, need to add XDEBUG_MODE=coverage in start of php artisan command which will generate report XDEBUG_MODE=coverage php artisan test --coverage-html tests/reports/coverage – Rahul_Dange Jun 24 '22 at 15:06
3

first, you need to be sure that you have Xdebug installed, then you need to be sure this Xdebug is on coverage mode, you can run it by the below command (which makes it on during running it)

php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-clover='reports/coverage/coverage.xml' --coverage-html='reports/coverage'

Please consider that Xdebug is not designed for generating reports, so in case you want to generate a report for a huge number of tests you will face with process killed problem from OS, In that case, you should look for other packages

Mhmd
  • 406
  • 5
  • 12