go-bcov
go-bcov
is a tool to calculate branch coverage from Go coverage reports.
It works by reading the coverage file generated by go to get the line coverage
and then concurrently parses the package AST to understand which branches
have been covered. Both if
and switch
statements are supported.
The coverage file is read from standard input and the report is generated to standard output.
Installation
Preferred
go install github.com/alx99/go-bcov@v1
Prebuilt binaries
You can download binaries from the releases page.
Note that this project has a dependency on the go
tool, so you will need to have it installed.
If you face an error like go/build: go list fork/exec /opt/hostedtoolcache/go/1.23.1/x64/bin/go: no such file or directory
when executing the prebuilt binary, please use the preferred installation method or try the following workaround:
GOROOT="$(go env GOROOT)" ./go-bcov
Usage
Sonarqube coverage report
go test -coverprofile=coverage.out -covermode count ./...
go-bcov -format sonar-cover-report < coverage.out > coverage.xml
# and upload...
sonar-scanner-cli \
-Dsonar.sources=. -Dsonar.exclusions=**/*_test.go,**/*_mock.go \
-Dsonar.tests=. -Dsonar.test.inclusions=**/*_test.go \
-Dsonar.coverageReportPaths=coverage.xml -Dsonar.go.coverage.reportPaths=coverage.txt