Laravel Tip: Store your test coverage with CodeCov
Tags: php, codecoverage, laravel, softwaredevelopment
Published: 2022-12-09 21:08:03

---
title: Laravel Tip: Store your test coverage with CodeCov
published: true
date: 2022-12-03 17:55:45 UTC
tags: php,codecoverage,laravel,softwaredevelopment
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/0*RLpiHY11ptVR8JDJ)
_Photo by Ricardo Resende on Unsplash_

Before getting started, I'd love for you to consider checking out my latest venture [FlagFox](https://www.flagfox.dev/?utm_campaign=waitlist&utm_source=devto&utm_content=codecov).

[![](https://www.flagfox.dev/img/github-readme-image.png)](https://www.flagfox.dev/?utm_campaign=waitlist&utm_source=devto&utm_content=codecov)

### Introduction

If you’re not aware Laravel’s artisan test command has been able to generate code coverage reports in the console output for some time now. This is great, but receiving that output in the console only helps a little when you run many of your tests in continuous integration.

This is where [CodeCov](https://about.codecov.io/) comes up, a service for keeping track of your application's code coverage over time.

This tutorial will cover Setting up CodeCov and creating a GitHub Action to store the results. Nothing is stopping you from using another CI service with CodeCov, but for publishing results, I'll be using specifically GitHub actions.

### Create a CodeCov account

This is straightforward as setups go. You can create an account via GitHub, which makes sense if you're already hosting your repo with GitHub.

![](https://cdn-images-1.medium.com/max/1024/1*kOldAeVjyESa9EPc_UtB0Q@2x.png)

At that point, you can search for the git repo you want to collect code coverage for, and upon selecting that repository, you'll see the following screen.

![](https://cdn-images-1.medium.com/max/1024/1*MQz7aRxJV4OB44Au1xvIog@2x.png)

This will stay blank until we start pushing any coverage reports. To do this, we need to create our testing action. On this page, you will see a Token that you'll need to copy if you're setting up a _private_ GitHub repository. Open-source projects can skip the token setup.

### Creating a GitHub Action

The first thing to do will be to set up a secret in your GitHub repository. This will be the safest place to keep the token so others won't be able to upload code reports to CodeCov.

![](https://cdn-images-1.medium.com/max/1024/1*hXtlbAtJapN_lkfIehelUg@2x.png)

You only need to create a new Actions Secret in the repository settings, copy the token into the Secret input, and then save it.

The next part all occurs within our code.

We need to add a .github/workflows/run-tests.yml file to our project's folder. It also should contain the following Yaml file.

{% gist https://gist.github.com/peterfox/4c60a93ae87f3253e5321adebf576c1e %}

Please note that sometimes your tests will have different requirements; you will need to make that work before sending coverage reports. What's important here is the following:

1. Your PHP setup must have a code coverage driver. In the example, it's PCov which, at the moment, is considered the best option for running tests with speed.
2. When we reach the code execution step, we're running PHPUnit (or, in this case, the Artisan Test command) with the coverage options and telling PHPUnit where to save the report.
3. In the project, I've added a /storage/coverage folder and put a .gitignore file so that reports can be generated to that folder but will never be committed to the repository.
4. We're using the [CodeCov Github Action](https://github.com/codecov/codecov-action) they provide to use the token from the secrets we've configured, and we've told it the file we want to upload.

You’ll find that if you have set up the secrets or the actions wrong, the CodeCov upload step will fail silently and require you to look at the action's output to explain why it's failed.

If you've been successful, your code coverage action will look like this:

![](https://cdn-images-1.medium.com/max/1024/1*nv_nmwKwclYuIQVW5CyFhA@2x.png)

Now when we go to CodeCov, we should be able to see the output of our actions.

![](https://cdn-images-1.medium.com/max/1024/1*Voe9Yut73QMXDi9OH5CD-g@2x.png)

You can [browse the results on CodeCov](https://app.codecov.io/gh/peterfox/laravel-codecov-demo) directly for the [demo project](https://github.com/peterfox/laravel-codecov-demo) I created.

### Adding that coverage Badge

Of course, what is the point of this all if you won't be showing off that (hopefully green) code coverage?

To do this, we only need to go to the settings of our repo on CodeCov, and then we can copy the provided Markdown.

![](https://cdn-images-1.medium.com/max/1024/1*QnKy2rd-CssYKGvp0ABJwQ@2x.png)

Once we have this, we can paste it into our project's README.md file.

![](https://cdn-images-1.medium.com/max/1024/1*ea3fa4M89mO2w08L7Ywzvg@2x.png)

Once committed, we'll see the badge and click through to the report on Codecov.

![](https://cdn-images-1.medium.com/max/1024/1*PaSml_kHK-Rca5iVxgRQkw@2x.png)

### Conclusion

Well, we can wrap this up quickly. Code coverage is a great metric to monitor, don't put too much faith in that 100% mark, instead treat the value as a way to track progress or regression. Suppose you find yourself starting with 50% or so. Then it would be best if you aimed to maintain or improve this value. If you see it dropping over the next few months, you can begin to question whether you need to evaluate why that is the case or not.

Either way, I hope you'll have fun getting that feedback. Feel free to tweet me your code coverage on [Twitter](https://twitter.com/SlyFireFox); I'd love to hear what you got.

Like Always, if you want to see all the code, you can [view the repository created on GitHub.](https://github.com/peterfox/laravel-codecov-demo)

I’m Peter Fox, a UK software developer with a decade of experience with PHP and Laravel. I’ve started work on a new project. Flagfox for Laravel will build upon my already established work creating open-source packages to help developers add feature flags to their applications. We currently have a [waiting list](https://www.flagfox.dev/?utm_campaign=waitlist&utm_source=devt&utm_content=codecov#waitlist) for those interested in learning more.