'How to measure test coverage in Go
Has anyone succeeded in generating code coverage for Go unit tests? I can't find a tool for that on the web.
Solution 1:[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:

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.
Solution 2:[2]
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.
Solution 3:[3]
Simply run
go test -cover
or
go test -cover ./...
or
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
Solution 4:[4]
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=coverage.out -o coverage.html. Open the coverage.html file hereby generated in the browser and analyze the detailed coverage info.
Solution 5:[5]
It's right here, some docs here.
$ go tool
6a
6c
6g
6l
addr2line
api
cgo
cov
dist
ebnflint
fix
gotype
nm
objdump
pack
pprof
prof
vet
yacc
$ go tool cov -h
usage: cov [-lsv] [-g substring] [-m minlines] [6.out args...]
-g specifies pattern of interesting functions or files
go tool cov: exit status 1
$
I haven't used it, this is all I know.
Solution 6:[6]
If you like to see the uncovered lines by function directly in a terminal I rewrote the cover tool for this purpose. It's available at https://github.com/gregoryv/uncover.
Usage
go get -u github.com/gregoryv/uncover/...
go test -coverprofile /tmp/c.out
uncover /tmp/c.out
Screenshot
Solution 7:[7]
If you are using VSCode this functionality is supported out the box ( But disabled by default )
Just turn on test on save + coverage reporting
https://github.com/microsoft/vscode-go/wiki/On-Save-features
It will even show in your editor which lines are not covered which is super handy.
Solution 8:[8]
A quick and easy way is to use the coverage tool that comes with built-in go :
$ go test -coverprofile cp.out // Emits the coverage in one liner percentage wise
After you execute the above command, if you wish to visually see the code coverage (like covered statements and missed etc)
$ go tool cover -html=cp.out
Note : You need to execute the above commands in the folder where you wish to see coverage
Solution 9:[9]
Inspired by the help menus and other answers to this question, just run:
f=cover.out; if [ -f $f ]; then rm $f; fi; go test ./... -coverprofile $f && \
go tool cover -html $f && \
rm $f
Solution 10:[10]
If you want to find test coverage in Windows, just go to the desired folder in command prompt and type the following command:
go test -coverprofile=coverage.out && go tool cover -html=coverage.out
This is perfectly easy and works reliably.
Solution 11:[11]
I can't find a tool for that on the web.
Actually... there is now (2022) such a tool on the web, from Nikolay Dubina's project go-cover-treemap-web:
Nothing it uploaded (the processing remains local), but by dragging/dropping your coverprofile, the Web UI (using Go WASM) will run go-cover-treemap and display:

(gocovergage for https://github.com/gohugoio/hugo)
Solution 12:[12]
Try using gaia-docker/base-go-build Docker Image.
This is a Docker image that contains all you need in order to build and test coverage. Running test coverage inside a Docker container creates .cover folder with test coverage results of your project.
docker run --rm -v "$PWD":$PROJECT_PATH -w $PROJECT_PATH $BUILDER_IMAGE_NAME /go/script/coverage.sh
The test coverage script running on all projects' folders and generates, inside .cover folder junit and coverage reports for each folder, and a combine coverage report of all projects' tests.
Codecov also suggests a script that collect coverage results: multiple files
Solution 13:[13]
Test Coverage for Golang
go get github.com/axw/gocov/gocov
go get -u gopkg.in/matm/v1/gocov-html
Check It is Installed Correctly And you have access from your Terminal
Run the Test Case
If you run the test case it will Reder the .json File Based on the file you will get the Code Coverage Report in .html file
gocov test >your_Coverage_report.json
Once Your Test case is done Generate a Report in .html File using .json
gocov-html your_Coverage_report.json >your_Coverage_report.html
Reference
GoTest Coverage Tool for go lang
Alternate Method
Go Native Test coverage
go test -coverprofile=coverage.out
go tool cover -html=coverage.out
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow

