Introduction: What is Test Coverage, and Why is it Important?
We all know tests are crucial, but how many should you write? Should you aim for 100% coverage or just focus on critical parts? While there’s no definitive answer, tracking your codebase’s test coverage percentage is a valuable guide. Think of it like monitoring your weight: you don’t have to step on the scale every day, but it helps to check in occasionally to ensure you’re on track.
Monitoring test coverage helps you see if the code you’re introducing is increasing or decreasing coverage. If a new feature causes a significant drop in coverage, it’s a signal to consider adding more tests–unless there’s a good reason not to, like dealing with a difficult-to-test third-party library.
Many languages like Go and Jest offer built-in tools for generating detailed coverage reports. But what if you want to automate the comparison between your main branch and a new pull request (PR) to see how it impacts test coverage? Tools like Codecov and Code Climate offer this feature, leaving helpful comments on your PRs with the coverage impact.
However, if budget constraints are an issue, you can build this feature yourself using GitHub Actions and Artifacts. This approach lets you automatically compare coverage changes for every PR against the main branch, making it easier to maintain high testing standards without the added cost.
Overview of the Approach
At a high level, we’ll create two separate GitHub Actions files: test-main.yml and test-pr.yml. The test-main.yml file is triggered on every push to the main branch, where it runs tests, generates a coverage report, and stores it as a GitHub Artifact. The test-pr.yml file runs on every PR to main, generates its own coverage report, compares it to the main branch report, and adds a comment to the PR with the resulting percentage difference.
Below, I’ll dive into the implementation details for both Go and React projects.
Automating Test Coverage Reporting for Go
Setting Up the test-main.yml File
The test-main.yml file is designed to run whenever there’s a push to the main branch. Here’s how you can set it up:
When there is a push to main, this workflow will run tests, generate a code coverage report, and store it as a GitHub Artifact. For example:
Setting Up the test-pr.yml File
The test-pr.yml file will run on every PR to main and compare the coverage of the PR branch against main. Here’s how to configure it:
This workflow starts by running tests and generating a coverage report for the PR branch:
Next, we compare this report with the one from main:
Finally, we create a PR comment that shows whether the test coverage has increased or decreased:
Here’s an example of what the comment might look like:
Automating Test Coverage Reporting for React
Setting Up the test-main.yml File
For React projects, the approach is similar, but we use Jest’s built-in coverage tools. The test-main.yml file will look something like this:
Setting Up the test-pr.yml File
The test-pr.yml file for React projects follows a similar structure to the Go example, but with adjustments for Jest:
Finally, we create a comment on the PR similar to the Go setup:
Conclusion
Automating test coverage reporting with GitHub Actions helps ensure your code remains robust without the overhead of manual checks. Whether using Go or React, this approach can be adapted to fit your project’s needs. The process enhances code reviews by providing clear insights into how each PR impacts overall test coverage.
There are many other features that Codecov and Code Climate offer, such as badges for your Readme and detailed web-based coverage reports. While these are great, building your own solution can give you more control and save costs. However, as these tools evolve, it may be worth considering their paid options for additional functionality, especially with new AI features being introduced.