Go test coverage
SonarQube Server supports the reporting of test coverage information as part of the analysis of your Go project.
However, SonarQube Server does not generate the coverage report itself. Instead, you must set up a third-party tool to produce the report as part of your build process.
You then need to configure your analysis to tell the SonarScanner where the report is located so that it can pick it up and send it to SonarQube Server, where it will be displayed on your project dashboard along with the other analysis metrics.
For Go projects, SonarQube Server supports the native Go test tooling.
Adjust your setup
To enable coverage for Go, you need to:
- adjust your build process so that the coverage tool generates the report(s) just after your unit as part of the clean build required to run analysis
- make sure that the coverage tool writes its report file to a defined path in the build environment
- configure the scanning step of your build so that the scanner picks up the report file from that defined path
Adding coverage to your build process
The first step is to generate the coverage reports.
The simplest way to generate a report is to run your test with the -coverprofile=<location>
flag.
This will tell the Go tooling to generate a coverage report file at a specific location. For example, go test -coverprofile=coverage.out
should generate a coverage.out
report in the working directory.
Adding the coverage analysis parameter
The next step is to add sonar.go.coverage.reportPaths
to your analysis parameters. This parameter must be set to the path of the report file produced by your coverage tool. The parameter supports the mention of multiple coverage file reports separated by a comma.
In this example, that path is set to the default. It is set in the sonar-project.properties
file, located in the project root:
sonar.projectKey=<sonar-project-key>
...
sonar.go.coverage.reportPaths=coverage.xml
Coverage parameters can be set in multiple places
As with other analysis parameters, sonar.go.coverage.reportPaths
can be set in multiple places:
- In the
sonar-project.properties
file, as mentioned above. - On the command line of the scanner invocation using the
-D
or--define
switch, for example,
sonar-scanner -Dsonar.go.coverage.reportPaths=coverage.xml
- In the SonarQube Server interface, in the Path to coverage report(s) section under:
- Your Project > Project Settings > General Settings > Languages > Go for project-level settings
- Your Project > Administration > Configuration > General Settings > Languages > Go for global settings (applying to all projects).
Related pages
Was this page helpful?