Interesting Go Modules 02: Coverage with courtney

Published by

on

In any coding ecosystem, ensuring high-quality code coverage is part of maintaining reliable and bug-free applications. In Go, one of the standout tools for code coverage testing is courtney, a module designed to enhance Go’s testing capabilities by providing developers features that the stdlib coverage tool does not offer.

In this post, we’ll dive into what courtney is, why it’s a valuable addition to any Go developer’s toolkit, and the features it offers to help you get the most out of your tests.


courtney is a code coverage analysis tool for Go that extends the capabilities of Go’s built-in testing framework. While Go’s native testing library can provide basic coverage statistics, courtney offers a more intuitive, insightful breakdown of your code’s coverage status.

Here’s why it stands out:

  • Granular Coverage Data: courtney provides a fine-grained look into which specific lines of code are executed during tests and which aren’t, giving you a precise idea of what’s covered.
  • Built In Coverage Ignoring: courtney automatically skips parts of your code that should not be included in final coverage reports. For example, areas of code where all you are doing is checking if err is nil and then simply propagating the error by returning it further up the stack.
  • Coverage Skipping Tool: courtney also gives you the ability to skip entire files or branches of code in coverage calculations allowing you to focus more on reporting what truly matters.

How to Use Courtney

Courtney is designed to fit seamlessly into a Go developer’s workflow. Here’s a quick guide on how to get started:

  1. Install Courtney
    • You can install courtney easily with: go install github.com/your/courtney
  2. Run Coverage Analysis
    • courtney makes it simple to run a full coverage analysis: courtney -v ./...
  3. View Coverage Report
    • Passing the -v verbose flag prints test output to the console. Of course, you can also also choose to integrate other coverage report tooling like sense courtney still produces the same coverage.out file that the stdlib coverage tooling does.

Conclusion

Integrating courtney into your Go projects can elevate your testing practices and ensure your codebase stays robust over time. Being able to skip entire files and blocks of code in your coverage calculations by simply adding a //notest comment makes it extremely useful when trying to achieve minimum coverage thresholds without having to test meaningless parts of your code like mocks =)

Lastly, courtney makes use of the stdlib go test command and accepts flags that it passes into go test meaning you can still leverage go test the way you usually would in most cases and transitioning from stdlib coverage to courtney is not painful.

To learn more please see the repo here.

Cheers!

Leave a comment