Laravel not generating code coverage report
Asked Answered
L

4

11

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?

Lala answered 30/3, 2021 at 18:11 Comment(1)
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).Photodisintegration
T
6

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
Tapeworm answered 16/2, 2022 at 14:9 Comment(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/coverageVial
From Laravel doc it's specified Xdebug or PCOV. What about PCOV? laravel.com/docs/10.x/testing#reporting-test-coverageNerissa
T
7

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
Turino answered 17/6, 2021 at 16:28 Comment(0)
B
6

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

Bradberry answered 2/2, 2022 at 11:44 Comment(3)
From Laravel doc it's specified Xdebug or PCOV. What about PCOV? laravel.com/docs/10.x/testing#reporting-test-coverageNerissa
Hey Jean, I havent checked the newer version of Laravel yet unfortunately, I stocked with Laravel 8, but please let me know if you figured it outBradberry
yes, at least with Laravel 9, I didn't tried in L8, but install pcov, disable xdebug and run phpunit with code coverage option. It seems not be implemented with artisan and Laravel 8 and 9, so just run it with phpunit. Cf: version-compatibilityNerissa
T
6

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
Tapeworm answered 16/2, 2022 at 14:9 Comment(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/coverageVial
From Laravel doc it's specified Xdebug or PCOV. What about PCOV? laravel.com/docs/10.x/testing#reporting-test-coverageNerissa
F
0

I setup phpunit, xdebug. Tests were passing but still it didn't generate files. My issue was because of php error in one of my file.

I see this error once using php artisan test --coverage

Fouts answered 11/3 at 8:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.