Home > database >  How to calculate total code coverage for tests
How to calculate total code coverage for tests

Time:11-15

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

CodePudding user response:

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%

CodePudding user response:

Judging from output of go help build - no, you cannot build an executable that is instrumented to collect coverage information for different packages of your project.

  • Related