run all test except vendor

1
go test $(go list ./... | grep -v /vendor/)

go tool cover

1
2
3
go test -coverprofile coverage.txt <package name>
go tool cover -html=coverage.txt -o coverage.html
chrome coverage.html

example script to run all package test and generate coverage file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env bash
set -e
out="coverage.txt"
tmp="profile.coverage.out"
except="example|testproto"
: > $out
i=0
for d in $(go list ./... | grep -v vendor); do
# except some package
if [[ $d =~ $except ]]; then
continue
fi
echo -e "TESTS FOR: for \033[0;35m${d}\033[0m"
go test -v -coverprofile=$tmp $d
if [ -f $tmp ]; then
# remove mode:xxx if not first package for go tool cover
if [ "$i" != 0 ]; then
sed -i '' -e '/^mode:/d' ./$tmp
fi
cat $tmp >> $out
rm $tmp
fi
echo ""
((i++))
done