Using the -cover
option of go test
, eg
go test ./... -covermode=atomic -coverprofile coverage.out
coverage.out
contains lots of output for each file, but I want a single number for overall coverage (which can be used to pass/fail coverage).
I found a reasonable (although erroneous!) script solution in this article:
cat coverage.out | \
awk 'BEGIN {cov=0; stat=0;} \
$3!="" { cov+=($3==1?$2:0); stat+=$2; } \
END {printf("Total coverage: %.2f%% of statements\n", (cov/stat)*100);}'
but is there a non-script, go
way to do this?