Display Build Status and Test Coverage Badges for a Laravel Project

Image for post
Image for post

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

Image for post
Image for post

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.

"php-coveralls/php-coveralls": "^1.1"

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.

coverage_clover: build/logs/clover.xml
json_path: build/logs/coveralls-upload.json
service_name: travis-ci

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

[![Build Status](https://travis-ci.com/chyke007/credible.svg?branch=master)](https://travis-ci.com/chyke007/credible)

Coveralls Code Coverage Badge

[![Coverage Status](https://coveralls.io/repos/github/chyke007/credible/badge.svg?branch=master)](https://coveralls.io/github/chyke007/credible?branch=master)

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!

Written by

Full-stack developer, technical writer and lover of Jamstack. Passionate about lifelong learning.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store