Running tests and getting coverage reports on server deployment in SnailLife

Yesterday I added tests to server deployment in my script:

echo "Running tests"
cd ../../server/lib

set -e
echo "mode: set" > allcoverage.out

for d in $(go list ./... | grep -v vendor); do
    parentdir=`dirname "$d"`
    subdir=`basename "$d"`
    echo "subdir: " $subdir
    if [[ $subdir == "tests" ]]; then
        go test -cover -coverpkg=$parentdir -coverprofile=profile.out $d
        go test -cover -coverprofile=profile.out $d

    if [ -f profile.out ]; then
        tail -n+2 profile.out >> allcoverage.out
        rm profile.out

Basically this goes into the root where all of my server packages live. Then for each found package we get the subdirectory name and the full path of the parent directory. If the subdirectory is named “tests” (most of my tests are in packages under the package I’m actually testing), we run go test -cover with -coverpkg specified as the parent dir of the test dir. Otherwise we do not specify -coverpkg because it is in the same directory as the test.

At the end we get an allcoverage.out file which can be opened in the browser to view coverage for each tested package:

SnailLife test coverage report

© - 2021 · Liza Shulyayeva ·