Code coverage in Golang- Answered

What is code coverage?

Code coverage is a measurement of how many lines/blocks/arcs of your code are executed while the automated tests are running.

Code coverage is collected by using a specialized tool to instrument the binaries to add tracing calls and run a full set of automated tests against the instrumented product. A good tool will give you not only the percentage of the code that is executed but also will allow you to drill into the data and see exactly which lines of code were executed during a particular test.

Just remember, having “100% code coverage” doesn’t mean everything is tested completely – while it means every line of code is tested, it doesn’t mean they are tested under every (common) situation.

How to measure code coverage in Golang?

Go comes with awesome tools for testing and coverage. Although all Go tools are well documented go tool cover -help I would suggest reading The cover story article on the official Go blog. It has plenty of examples and I strongly recommend it!

I have this function in my ~/.bash_profile. (you can just paste it in the terminal to give it a try).

cover () { 
    go test -coverprofile=$t $@ && go tool cover -html=$t && unlink $t

Then just cd into a go project/package folder and type cover. This opens a visual tool in browser which shows you the tested and untested code for each file in the current package. Very useful command! I strongly recommend it for finding what is not 100% tested yet! The shown results are per file. From a drop down in top-left you can see results for all files.

With this command you can also check the coverage of any package for example:

cover fmt

The output in terminal from this command would be:

ok      fmt 0.031s  coverage: 91.9% of statements

In addition to that in your browser you will see this tool showing in red all lines of code which are not covered with tests:

It is also possible to just save the html coverage file instead of opening it in a browser. This is very useful in cases when your tests + coverage is run by CI tool like Jenkins. That way you can serve the coverage files from a central server and the whole team will be able to see the coverage results for each build.

Alternative solution

Simply run

go test -cover


go test -cover ./...


go test -coverprofile=coverage.out ./... ;    go tool cover -func=coverage.out

or to check the source code

go test -coverprofile=coverage.out ./... ;    go tool cover -html=coverage.out

How to check code coverage in Go using the IntelliJ idea?

Code coverage results are displayed in tool windows and in the editor. The tool windows show the following information:

  • For a directory: the percentage of covered files and statements.
  • For a file: the percentage of the covered statements.

When a file is opened in the editor, each line is highlighted with regard to its code coverage status:

  • Lines executed during simulation are marked green.
  • Lines not executed during simulation are marked red.

The coverage measurement results comprise a coverage suite. You can have the results of a new simulation merged with any existing suite. In this case, a line will be considered covered if it is covered by at least one of the simulations.

A coverage suite is generated every time a test or application with code coverage measurement is executed. It is possible to have an unlimited amount of coverage suites.

Run with code coverage

General steps for using code coverage in a project

  1. Specify how you want to process the coverage results.
  2. Create a run/debug configuration for the target code, if you are going to measure code coverage for testing.
  3. Configure code coverage measurement in the desired run/debug configuration.
  4. Run with coverage, using the dedicated command from the main menu Run | Run with Coverage, or click the Run with Coverage button the Run with Coverage button.
  5. Once the run with coverage has been executed, you can perform the following actions:
    • Use the various coverage suites.
    • View code coverage data.

Answer #4:

Since version 1.2 code coverage is built into Go (Golang). Use go test -cover . to get basic coverage statistics for a single package.

$ go test -cover .
ok      calc        0.001s  coverage: 100.0% of statements

Unfortunately, it does not show the total coverage for all your packages within a single project. To get the overall code coverage for multiple packages you have to create a cover profile and use a second command go tool cover. First of all generate the coverage profile by using the -coverprofile flag instead of the -cover one and tell the command where to store the information.

$ go test ./... -coverprofile cover.out
ok      calc        0.001s  coverage: 100.0% of statements
ok      calc/format 0.001s  coverage: 100.0% of statements

Afterward use go tool cover with the -func flag and point it to the previously generated file. This will show you the code coverage for every single package within your project and down at the bottom the total coverage.

$ go tool cover -func cover.out
calc/calc.go:4:              Add             100.0%
calc/calc.go:9:              Subtract        100.0%
calc/calc.go:14:             Multiply        100.0%
calc/format/format.go:8:     Print           100.0%
total:                       (statements)    100.0%

This is great and the last line is the most important one for us. To only print this line use grep and filter for total:. This removes all the noise and returns only the total code coverage.

$ go tool cover -func cover.out | grep total:
total:                       (statements)    100.0%

The whole line is not really useful for automated processing. We only need the last value. By using awk we can easily deal with tabular data and make sure we’re only getting the third string.

$ go tool cover -func cover.out | grep total | awk '{print $3}'

We’re almost done here. However, for storing this value in our database we have to remove the percentage %. Luckily awk has substr built-in and we can simply remove the last character. We have to keep in mind that the total code coverage could vary between 0% and 100%. Therefore the initial string might be between 2 and 4 characters long. To handle the unknown string length we will use the built-in length method.

$ go tool cover -func cover.out | grep total | awk '{print substr($3, 1, length($3)-1)}'

Now we’re done. We’ve got our total coverage as a plain number.

Go Code Coverage HTML Report

We can get the coverage report in a graphical way via HTML. First, we need to set the cover profile. To do that use the command as shown below:

1go test -coverprofile=coverage.out    // coverage.out is the output filename

Now, we can use the following command to generate a graphical coverage report.

1go tool cover -html=coverage.out

The coverage graphic will look like this:

As can be seen, the graphical way of viewing the coverage is a pretty handy feature Go provides.

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →