Laravel - HTML Test Coverage Report

Irving Gomez | | Backend
Cover image for Laravel - HTML Test Coverage Report

HTML Test Coverage Report

In this tutorial I will be showing you how to set up test coverage reporting in Laravel with an HTML document output. This tutorial assumes you have a working PHP installation. Commands are being ran inside your Laravel projects main directory.

  1. Installing XDebug
    • To get going we first need to install the XDebug PHP extension. This can be easily achieved by using PECL. PECL is a repository of PHP extensions. Do not get this confused with PEAR, which is a repository of PHP packages.
    • pecl install xdebug
  2. Edit PHP.ini
    • If you don't know where your php file is, this command calls the PHP binary, and tells it to give us our configuration paths.
    • php --ini
    • Look for the line with "Loaded Configuration File:" and open it with your favorite editor, or with vim. In my environment, the INI file is located here.
    • /usr/local/etc/php/8.3/php.ini
    • You can find the file in your Finder by pressing COMMAND + SHIFT + G and in the path input, type in or paste the INI location or use vim vim /usr/local/etc/php/8.3/php.ini.
    • Add the following line to the end of the file. This tells XDebug to turn on the following features.
    • xdebug.mode=develop,coverage,debug,gcstats,profile,trace
  3. Generating the report
    • To generate the report, we need to call the test command, and include some extra coverage options.
    • php artisan test --coverage --coverage-html=./.cov
  4. Results
    • To view the HTML files, open the index file in chrome.
    • open ./.cov/index.html

That is it! You now have your test coverage report inside of your browser for easy viewing and navigation.