Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all packages' code coverage together in Go?

I have a library consisting of several packages. When running tests, I am using '-cover' flag and its showing the coverage information for each package individually.Like follows:

--- PASS: TestSampleTestSuite (0.00s) PASS coverage: 28.7% of statements ok      github.com/path/to/package1 13.021s ?       github.com/path/to/package2 [no test files]  === RUN   TestAbc --- PASS: TestAbc (0.43s) PASS coverage: 27.7% of statements 

Is there any way to get a full coverage overview easily to get good idea about coverage on the whole project?

Update: Here is the go test command I am using

go test ./... -v -short -p 1 -cover 
like image 876
Rana Avatar asked Oct 30 '15 21:10

Rana


People also ask

How do I get code coverage in Golang?

General steps for using code coverage in a project Specify how you want to process the coverage results. Create a run/debug configuration for the target code, if you are going to measure code coverage for testing. Configure code coverage measurement in the desired run/debug configuration.

How do I get go coverage?

To do test coverage in Go we create a test file by adding a _test suffix. Then we simply use test functions in that file. To generate the coverage report, the -cover flag is added after that. This cover flag tells the test tool to generate coverage reports for the package we are testing.

How is code coverage calculated?

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.

How do I ignore generated files from Go test coverage?

By using "_test" in the name definitions If you would like to ignore files which are used in the tests then make sense use _test at the end of the name. For example, my_service_mock_test.go . The files with _test at the end of the name will be ignored by default.


1 Answers

EDIT: Things have changed since I wrote this answer. See the release notes of Go 1.10: https://golang.org/doc/go1.10#test :

The go test -coverpkg flag now interprets its argument as a comma-separated list of patterns to match against the dependencies of each test, not as a list of packages to load anew. For example, go test -coverpkg=all is now a meaningful way to run a test with coverage enabled for the test package and all its dependencies. Also, the go test -coverprofile option is now supported when running multiple tests.

You can now run

go test -v -coverpkg=./... -coverprofile=profile.cov ./... go tool cover -func profile.cov 

Old answer

Here is a bash script extracted from https://github.com/h12w/gosweep :

#!/bin/bash set -e  echo 'mode: count' > profile.cov  for dir in $(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -type d); do if ls $dir/*.go &> /dev/null; then     go test -short -covermode=count -coverprofile=$dir/profile.tmp $dir     if [ -f $dir/profile.tmp ]     then         cat $dir/profile.tmp | tail -n +2 >> profile.cov         rm $dir/profile.tmp     fi fi done  go tool cover -func profile.cov 
like image 161
HectorJ Avatar answered Oct 05 '22 07:10

HectorJ