I need to calculate code coverage for golang project where source of tests will be integration tests written in Java language . This requires go build to be instrumented first and then run on server so that tests can run and we will get to know after tests have ended, how much is code coverage? I haven't found a single reference for this on internet all there is present is unit tests which can be run easily and used to calculate coverage
First of all generate the coverage profile by using the -coverprofile flag and tell the command where to store the information.
% go test ./... -coverprofile cover.out
? some.project/somepkg [no test files]
ok some.project/somepkg/service 0.329s coverage: 53.3% of statements
Than use go tool cover with the -func flag and point it to the previously generated file. This will show you the code coverage for every single package withing your project and down at the bottom the total coverage.
% go tool cover -func cover.out
some.project/somepkg/service/.go:27: FuncA 100.0%
some.project/somepkg/service/service.go:33: FuncB 100.0%
some.project/somepkg/service/service.go:51: FuncC 0.0%
total: (statements) 53.3%
% go tool cover -func cover.out | fgrep total:
total: (statements) 53.3%
% go tool cover -func cover.out | fgrep total | awk '{print $3}'
100.0%
% go tool cover -func cover.out | fgrep total | awk '{print substr($3, 1, length($3)-1)}'
100.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With