How to Setup PHPunit Code Coverage in GitLab

In a previous post, i showed us how to set up automated testing for your PHP application. If you are using the .gitlab-ci.yml configuration i posted, chances are you will get the below notice in your build log.

No code coverage driver is available

The notice is as a result of missing Xdebug PHP extension.

To install and enable Xdebug, add the commands below to the before_script: section of your project .gitlab-ci.yml

Having done the above, go to your project settings and open the CI/CD pipelines screen. In the Test coverage parsing input field, add the regex below.

For code coverage data to be captured by GitLab, ensure you set --colors option to never like so:

You should now see test coverage data displayed in the build console and build listing page.

Code coverage data on build listing

Code coverage  on Gitlab

La Fin!