Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 999
Owen Kelvin Avatar asked Mar 30 '21 18:03

Owen Kelvin


3 Answers

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
like image 157
user16254389 Avatar answered Oct 17 '22 05:10

user16254389


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
like image 38
Manoj Avatar answered Oct 17 '22 06:10

Manoj


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

like image 29
Mhmd Avatar answered Oct 17 '22 06:10

Mhmd