How to measure code coverage in the Golang?

Has anyone succeeded in creating code coverage for Go unit tests? I can not find a tool for this on the Internet.

+89
unit-testing go testing code-coverage
May 09 '12 at 12:59
source share
10 answers

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

One of the main new features of go test is that now it can calculate and, with the help of a new separately installed program " go tool cover ", display the results of test coverage .

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

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

The cover tool does two things.

  • First, when the -cover flag is assigned to the โ€œ go test โ€, it starts automatically to rewrite the source for the package and insert toolkit instructions. Then the test is compiled and executed as usual, and basic coverage statistics are reported:
 $ go test -coverprofile fmtcoverage.html fmt ok fmt 0.060s coverage: 91.4% of statements $ 

Secondly, for more detailed reports, various โ€œgo testโ€ flags can create a cover profile file that can then be analyzed by the cover program invoked with the โ€œ go tool cover โ€.

Frank Chirar mentions :

Recent versions of Go (2013/09/19) use:

 go test -coverprofile <filename> <package name> 

Detailed information 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 -o cover.html opens cover.html in browser

I donโ€™t even want to wait until the browser opens, so I defined this alias:

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

What I just typed gc and I have a list of all the lines that are not yet covered (here: the line coverage.out not ending with " 1 ").

+104
Sep 20 '13 at 7:54
source share

Go comes with a terrific testing and coverage tool. Although all Go tools are well documented go tool cover -help I would suggest reading the cover article on the official Go blog . It has many examples, and I highly recommend it!

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

 cover () { t="/tmp/go-cover.$$.tmp" go test -coverprofile=$t $@ && go tool cover -html=$t && unlink $t } 

Then just cd to the project / package folder go and type cover . This opens a visual tool in the browser that shows you the verified and unverified code for each file in the current package. Very helpful team! I highly recommend that you find something that has not yet been 100% verified! Shown results for each file. From the drop-down list at the top left you can see the results for all files.

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

cover fmt

The output in the terminal of this command:

 ok fmt 0.031s coverage: 91.9% of statements 

In addition to this, in your browser you will see that this tool displays in red all lines of code that are not covered by tests:

enter image description here

You can also just save the html coverage file and not open it in a browser. This is very useful in cases where your tests + coverage is performed by a CI tool like Jenkins. Thus, you can maintain coverage files from a central server, and the entire team will be able to see coverage results for each assembly.

+33
Dec 04 '14 at 1:00 a.m.
source share

This is correct here , some documents 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 have not used it, thatโ€™s all I know.

+5
May 9 '12 at 13:22
source share

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

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

Please note that in the HTML file you will find a drop-down button that will direct you to all the files.

+4
Aug 01 '18 at 13:32
source share

If you like to see open lines by function directly in the terminal, I rewrote the tool for this purpose. Available at https://github.com/gregoryv/uncover .

Using
 go get -u github.com/gregoryv/uncover/... go test -coverprofile /tmp/c.out uncover /tmp/c.out 

Screenshot

enter image description here

+2
Aug 20 '18 at 19:16
source share

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 
+1
Nov 22 '15 at 15:51
source share

Try using gaia-docker / base-go-build Docker Image.

This is a Docker image that contains everything you need to build and test coverage. Running test coverage in a Docker container creates a .cover folder with test coverage results for your project.

 docker run --rm -v "$PWD":$PROJECT_PATH -w $PROJECT_PATH $BUILDER_IMAGE_NAME /go/script/coverage.sh 

test coverage scenario that works with all project folders and creates inside the .cover folder folders and coverage reports for each folder, as well as a combined test coverage report for all projects.

Codecov also offers a script that collects coverage results: multiple files

0
Jan 03 '17 at 8:49
source share

Test cover for Golang

go get github.com/axw/gocov/gocov go get -u gopkg.in/matm/v1/gocov-html

Make sure it is installed correctly and you have access from your terminal

Run test case

If you run a test case, it will re-read the .json file based on the file, you will get a report on code coverage in the .html file

gocov test >your_Coverage_report.json

After the test is complete, create a report in the .html file using .json

gocov-html your_Coverage_report.json >your_Coverage_report.html

Link

GoTest Coverage Tool for Go Lang

Test Report Tool

Alternative method

Go Native Test Coverage

 go test -coverprofile=coverage.out go tool cover -html=coverage.out 
0
Mar 01 '18 at 6:39
source share

A quick and easy way is to use the coverage tool that comes with the built-in Go:

$ go test -coverprofile cp.out // Emits coverage as a percentage of the liner

After executing the above command, if you want to visually see the coverage of the code (for example, covered statements, skipped, etc.)

$ go tool cover -html = cp.out

Note: you need to execute the above commands in the folder where you want to see the coverage

0
May 8 '18 at 17:48
source share

Coverage Report โ†’

a) Run all tests and turn on coverage โ†’ pass the test. / ... -coverprofile cover.out

b) Get coverage for individual functions, as well as general coverage โ†’ go to coverage of the tool -func coverage.

c) Look at the lines that were covered and those that were not covered by your tests โ†’ go to the instrumental cover -html = cover.out -o cover.html Open the cover.html file created in the browser and analyze the detailed information about the coverage.

0
Sep 26 '19 at 17:19
source share



All Articles