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

前端 未结 1 525
醉话见心
醉话见心 2020-12-06 05:14

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 f

相关标签:
1条回答
  • 2020-12-06 05:41

    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
    
    0 讨论(0)
提交回复
热议问题