Is it possible to post coverage for multiple packages to Coveralls?
Asked Answered
F

6

13

I want to track test coverage on a go project using Coveralls, the instructions for the integration reference using https://github.com/mattn/goveralls

cd $GOPATH/src/github.com/yourusername/yourpackage
$ goveralls your_repos_coveralls_token

However, this only posts the results for one package and running for packages in turn does not work as the final run overwrites all other runs. Has anyone figured out how to get coverage for multiple packages?

Frostwork answered 14/1, 2014 at 23:10 Comment(1)
Since Feb. 2018 and Go 1.10, it is possible: see my answer below.Saks
F
10

I ended up using this script:

echo "mode: set" > acc.out
for Dir in $(find ./* -maxdepth 10 -type d );
do
        if ls $Dir/*.go &> /dev/null;
        then
            go test -coverprofile=profile.out $Dir
            if [ -f profile.out ]
            then
                cat profile.out | grep -v "mode: set" >> acc.out
            fi
fi
done
goveralls -coverprofile=acc.out $COVERALLS
rm -rf ./profile.out
rm -rf ./acc.out

It basically finds all the directories in the path and prints a coverage profile for them separately. It then concatenates the files into one big profile and ships them off to coveralls.

Frostwork answered 15/1, 2014 at 16:2 Comment(0)
H
5

Taking Usman's answer, and altering it to support skipping Godep and other irrelevant folders:

echo "mode: set" > acc.out
for Dir in $(go list ./...); 
do
    returnval=`go test -coverprofile=profile.out $Dir`
    echo ${returnval}
    if [[ ${returnval} != *FAIL* ]]
    then
        if [ -f profile.out ]
        then
            cat profile.out | grep -v "mode: set" >> acc.out 
        fi
    else
        exit 1
    fi  

done
if [ -n "$COVERALLS_TOKEN" ]
then
    goveralls -coverprofile=acc.out -repotoken=$COVERALLS_TOKEN -service=travis-pro
fi  

rm -rf ./profile.out
rm -rf ./acc.out

Notice that instead of looking at every directory, I us the go list ./... command which lists all directories that actually get used to build the go package.

Hope that helps others.

** EDIT **

If you are using the vendor folder for Go v.1.6+ then this script filters out the dependencies:

echo "mode: set" > acc.out
for Dir in $(go list ./...); 
do
    if [[ ${Dir} != *"/vendor/"* ]]
    then
        returnval=`go test -coverprofile=profile.out $Dir`
        echo ${returnval}
        if [[ ${returnval} != *FAIL* ]]
        then
            if [ -f profile.out ]
            then
                cat profile.out | grep -v "mode: set" >> acc.out 
            fi
        else
            exit 1
        fi
    else
        exit 1
    fi  

done
if [ -n "$COVERALLS_TOKEN" ]
then
    goveralls -coverprofile=acc.out -repotoken=$COVERALLS_TOKEN -service=travis-pro
fi  
Hoskinson answered 31/10, 2015 at 6:15 Comment(0)
S
4

Has anyone figured out how to get coverage for multiple packages?

Note: with Go 1.10 (Q1 2018), that... will actually be possible.
See CL 76875

cmd/go: allow -coverprofile with multiple packages being tested

You can see the implementation of a multiple package code coverage test in commit 283558e


Jeff Martin has since the release of Go 1.10 (Feb. 2018) confirmed in the comments:

  • go test -v -cover ./pkgA/... ./pkgB/... -coverprofile=cover.out gets a good profile and
  • go tool cover -func "cover.out" will get a total: (statements) 52.5%.

So it is working!

Saks answered 10/11, 2017 at 23:13 Comment(2)
i can confirm that something similar to go test -v -cover ./pkgA/... ./pkgB/... -coverprofile=cover.out gets a good profile and go tool cover -func "cover.out" will get a total: total: (statements) 52.5%Pneumoencephalogram
@JeffMartin Thank you! I have included your comment in the answer for more visibility.Saks
A
2

I have been using http://github.com/axw/gocov to get my code coverage.

I trigger this in a bash script, in here I call all my packages.

I also use http://github.com/matm/gocov-html to format into html.

 coverage)
       echo "Testing Code Coverage"
       cd "${SERVERPATH}/package1/pack"
       GOPATH=${GOPATH} gocov test ./... > coverage.json 
       GOPATH=${GOPATH} gocov-html coverage.json > coverage_report.html

       cd "${SERVERPATH}/package2/pack"
       GOPATH=${GOPATH} gocov test ./... > coverage.json 
       GOPATH=${GOPATH} gocov-html coverage.json > coverage_report.html
     ;;

Hope that helps a little bit.

Aceto answered 15/1, 2014 at 15:4 Comment(1)
I ended up using something very similar, see my answerFrostwork
S
1

Here is a pure GO solution:

I create a library that may help, https://github.com/bluesuncorp/overalls

all it does is recursively go through each directory ( aka each package ), run go test and produce coverprofiles, then merges all profiles into a single one at the root of the project directory called overalls.coverprofile

then you can use a tool like https://github.com/mattn/goveralls to send it to coveralls.io

hope everyone likes

Straightjacket answered 4/8, 2015 at 3:16 Comment(0)
F
1

In Go 1.13, following command generates coverage for multiple packages

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

for html report

go tool cover -html=profile.cov -o cover.html
Feign answered 7/4, 2020 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.