Laravel 9 implement a new feature added by Nuno Maduro to get code coverage,
the command is php artisan test --coverage
This post explains how to set xdebug
locally in a MacOS
environment
In your local environment you can run
php -v
You should be able to see the PHP
version, but also more data like xdebug version
installed locally
If you have not installed XDEBUG
here you would find How to install xdebug
PHP.ini
file locationThen to check the php.ini
file location (it changes from different php versions) run
php --ini
In the second line the php.ini
file location
Grab the file location and edit the php.ini
file:
## Command to open files with vscode... it is easier
code /usr/local/etc/php/8.0/php.ini
## or
nano /usr/local/etc/php/8.0/php.ini
Then add at the very bottom this line:
xdebug.mode=develop,debug,coverage
Now you are able to run
php artisan test --coverage
and it should work and return a test coverage output
The test --coverage
command would return a percentage of coverage,
in order to exclude this files to just test your own code implementations in phpunit.xml
file
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
<exclude>
<directory suffix=".php">./app/Macros</directory>
<file>./app/Http/Middleware/Authenticate.php</file>
<file>./app/Http/Middleware/RedirectIfAuthenticated.php</file>
<file>./app/Http/Middleware/TrustHosts.php</file>
<file>./app/Http/Middleware/TrustProxies.php</file>
<file>./app/Providers/BroadcastServiceProvider.php</file>
<file>./app/Providers/HorizonServiceProvider.php</file>
<file>./app/Providers/TelescopeServiceProvider.php</file>
</exclude>
</coverage>
Thanks for reading! and may the code coverage be with you
Sign up & get tips and tricks
You'll get monthly updates regarding my most recent articles and products.