'How to get an overall coverage value from go test -cover? [duplicate]
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?
Solution 1:[1]
We us gotestsum for this purpose. It is a replacement for go test with additional features, like outputting in JUnit XML format.
https://github.com/gotestyourself/gotestsum
The disadvantage of using a third party tool like this is, that your developers and CI need to have it installed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jens |
