Display Build Status and Test Coverage Badges for a Laravel Project
In search for a new developer role, I found myself building various projects just to scale through multiple interviews. One in particular was a Laravel project that included a specific instruction to display both build status and code coverage report as badges in my readme.md file.
I searched for help online but couldn’t find any that gave the full guide to solving this problem, most only provided solutions for build status while ignoring the code coverage aspect. I had to figure this out and time was running out.
This below is what I intended to achieve and I would be showing how I accomplished this
Setup repo with Travis CI and Coveralls
If you haven’t already, the first thing you need to do is to move over to Travis and login using your GitHub account. Once logged in hit the “Add New Repository” button to activate your repo. Repeat this procedure at Coveralls.io.
Configure PHPUnit and Coveralls
Luckily for us, Laravel comes shipped with PHPUnit. This tutorial assumes you have some tests written already. If not, it’s as simple as checking out the Laravel docs for writing test.
We’re also going to install Coveralls, which we’ll use to host the coverage report by simply require it:
composer require php-coveralls/php-coveralls
Or by adding the following line to the require block of your composer.json file.
This would add it to our composer.json file as a dependency.
Create a .travis.yml and .coveralls.yml
We would now proceed to create a Travis configuration file(.travis.yml) at the root of our Laravel project to get Travis CI set up. Also we would be creating a .coveralls.yml which would be used to set up the path to our clover.xml
Here’s what my .travis.yml looks like: I’m setting the language, the version of php, and telling it which additional script to run.
Here’s what my .coveralls.yml looks like: I’m assuming your phpunit.xml saves its clover.xml at the build/logs path.
For the benefit of doubt, this is how my phpunit.xml and .env.example files looks like:
Deploy code and display badges
Here’s the code for both of the badges I used in my readme.md file. Both Travis and Coveralls will supply embed links for these.
Travis CI Build Badge
Coveralls Code Coverage Badge
Now once we push our code to GitHub with this setup it should send the build off to Travis, and Travis should send the coverage off to Coveralls.
That is all, in general it was an exciting adventure, hopefully this serves as a guide to someone, a guide I wish I had!