Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure test coverage in Go

People also ask

How do I run code coverage in Golang?

Run a test with code coverageOpen the desired file in the Project tool window and choose Run <name> with Coverage from the context menu. You can also select a directory with test files and choose the corresponding command from the context menu to run several tests with coverage.

How is test code coverage measured?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.

What is the tool used for to check test coverage?

TestMonitor. TestMonitor is an efficient tool for creating a visual appeal on the Test Coverage progression in the test management process and testing lifecycle.


Note that Go 1.2 (Q4 2013, rc1 is available) will now display test coverage results:

One major new feature of go test is that it can now compute and, with help from a new, separately installed "go tool cover" program, display test coverage results.

The cover tool is part of the go.tools subrepository. It can be installed by running

$ go get golang.org/x/tools/cmd/cover

The cover tool does two things.

  • First, when "go test" is given the -cover flag, it is run automatically to rewrite the source for the package and insert instrumentation statements. The test is then compiled and run as usual, and basic coverage statistics are reported:

    $ go test -coverprofile fmtcoverage.html fmt ok fmt 0.060s coverage: 91.4% of statements $

  • Second, for more detailed reports, different flags to "go test" can create a coverage profile file, which the cover program, invoked with "go tool cover", can then analyze.

Frank Shearar mentions:

The latest versions of Go (2013/09/19) use:

go test -coverprofile <filename> <package name>

Details on how to generate and analyze coverage statistics can be found by running the commands

$ go help testflag
$ go tool cover -help

Ivan Black mentions in the comments:

go test -coverprofile cover.out and then
go tool cover -html=cover.out opens cover.out in your default browser

I don't even want to wait for the browser to open, so I defined this alias:

alias gc=grep -v -e " 1$" cover.out

That I just type gc, and have a list of all the lines not yet covered (here: with a coverage.out line not ending with " 1").


Go comes with awesome tool 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 () { 
    t="/tmp/go-cover.$$.tmp"
    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:

enter image description here

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.


In addition to the good answers above, I find these three lines to be the simplest way to get it (which includes all packages):

go test -v -coverprofile cover.out ./YOUR_CODE_FOLDER/...
go tool cover -html=cover.out -o cover.html
open cover.html

Note that in the HTML file you will find a dropdown button that will direct you to all files.


Coverage Report →

a) Run all the tests and enable coverage --> go test ./... -coverprofile coverage.out

b) Get coverage for individual functions as well as overall coverage → go tool cover -func coverage.out

c) See the lines covered and the ones not covered by your tests → go tool cover -html=cover.out -o coverage.html. Open the coverage.html file hereby generated in the browser and analyze the detailed coverage info.