Code coverage when running PHPUnit on Laravel Homestead

This is a quick tip on how to get code coverage results when running PHPUnit on Laravel Homestead. This will enable Xdebug first and then run your tests.

Xdebug is not enabled by default

When you use the PHP CLI on Laravel Homestead, Xdebug is not enabled by default.

vagrant@homestead:~/Code/lastfm$ php -v
PHP (cli) (built: Aug  4 2017 13:04:12) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache, Copyright (c) 1999-2017, by Zend Technologies
    with blackfire v1.17.3~linux-x64-non_zts71,, by SensioLabs

So, when you run your PHPUnit tests it will tell you that no code coverage driver was found and no code coverage will be reported.

vagrant@homestead:~/Code/lastfm$ phpunit
PHPUnit 6.2.4 by Sebastian Bergmann and contributors.

Runtime:       PHP
Configuration: /home/vagrant/Code/lastfm/phpunit.xml
Error:         No code coverage driver is available

.......................                                           23 / 23 (100%)

Time: 308 ms, Memory: 6.00MB

OK (23 tests, 40 assertions)

Luckily, there's an easy solution for this.

How to enable Xdebug

There are 2 aliases that you can use to quickly enable and disable Xdebug: xon and xoff. As you would expect, xon enables Xdebug and xoff disables it again.

So, we can run our code coverage in the following way:

vagrant@homestead:~/Code/lastfm$ xon
vagrant@homestead:~/Code/lastfm$ phpunit
PHPUnit 6.2.4 by Sebastian Bergmann and contributors.

Runtime:       PHP with Xdebug 2.5.5
Configuration: /home/vagrant/Code/lastfm/phpunit.xml

.......................                                           23 / 23 (100%)

Time: 1.77 seconds, Memory: 8.00MB

OK (23 tests, 40 assertions)

Generating code coverage report in Clover XML format ... done

Generating code coverage report in HTML format ... done

Afterwards you can disable Xdebug again if you want to have a slightly better performance.

Usage outside Laravel Homestead

Both these aliases are available in Laravel Homestead by default. If you want to use them on you own machine, add the following lines to the .bash_aliases file in your home directory:

alias xoff='sudo phpdismod -s cli xdebug'
alias xon='sudo phpenmod -s cli xdebug'

Read some more

Comments (8)

Got a question? Liked the article or got a suggestion? Leave a comment to let us know.

Ah, just what I was looking for...
One addition: if you've already enabled xdebug in your php.ini `xoff` wont work.
Thanks for the comment Viktor! I've now added the actual aliases to the text for some extra clarification.
I'm trying to set this up with PHPStorm... I was able to get code coverage to run, but where does it save the results? PHPStorm doesn't know where to find them and wants me to "Choose Coverage Suite to Display"...
You can specify both the test suites and the location of any code coverage output in the PHPUnit configuration file.

Hi Barry,

thanks for this blog, I tired this solution but still not working for me I don't know why???
Hi Barry,

I fix it!!!
my php version was 7.2 in homestead I needed to move to 7.1 and it worked.
Glad to hear you got it working, Badreddine!
If you are using PHPunit with Laravel for unit testing, make sure to disable CSRF. If CSRF is enabled, you will get TokenMismatchException error. Source: