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.
go install github.com/alx99/go-bcov@v1
You can download binaries from the releases page.
Note that this project has a runtime dependency on go
, so you will need to have it installed.
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
- Sonarqube generic coverage report
- Both line and branch coverage are supported.