Golang code coverage difficulties

  • ALM used (GitLab)
  • CI system used (Gitlab CI)
  • Golang

I’m having a real hard time getting our code gates to be effective. Bugs, vulnerabilities, smells, all of these work fine.

Everything except code coverage.

I’ve built a simple demo repository, structured like so:

~/work/sonartest$ tree .
.
├── cmd
│   └── sonartest
│       ├── main.go
│       └── main_test.go
├── go.mod
├── Makefile
├── pkg
│   └── addpkg
│       ├── main.go
│       └── main_test.go
└── sonar-project.properties

Nothing too out of the ordinary, just a dumb hello world with a couple simple and easily tested functions.

I’ve tried so many different settings in my sonar-project.properties (like the sane default suggested here https://docs.sonarqube.org/latest/analysis/languages/go/ which should work just fine), but it always shows that we have 0.0% code coverage.

I’ve also tried manually specifying sonar.test.inclusions=cmd/sonartest/main_test.go just to see if I could reduce the number of allegedly uncovered lines, and I see no change.

I’ve seen some mention of using third-party tools to generate coverage reports, and then have sonar read those. Is that what I’m missing here?

Thanks,

– Ben

2 Likes

Hello, welcome to the community!

I’ve seen some mention of using third-party tools to generate coverage reports, and then have sonar read those. Is that what I’m missing here?

Exactly, SonarQube is not computing coverage itself. You can find a small guide here, and the list of SonarQube properties here.

To sum up, you will have to:

  • Run go test -coverprofile=coverage.out before the analysis.
  • Set sonar.go.coverage.reportPaths to list paths of coverage report files.

Hope this helps.

Quentin

3 Likes

@Quentin when I send those coverage reports to Sonar I get an error:

**16:26:26** INFO: Load coverage report from '/var/lib/jenkins/workspace/P-237288-code-coverage-sonarqube/tests/coverage/api.cov' **16:26:26** ERROR: Error parsing coverage info for file /var/lib/jenkins/workspace/P-237288-code-coverage-sonarqube/tests/coverage/api.cov: Invalid go coverage, expect 'mode:' on the first line. **16:26:26** INFO: Load coverage report from '/var/lib/jenkins/workspace/P-237288-code-coverage-sonarqube/tests/coverage/backend.cov' **16:26:26** ERROR: Error parsing coverage info for file /var/lib/jenkins/workspace/P-237288-code-coverage-sonarqube/tests/coverage/backend.cov: Invalid go coverage, expect 'mode:' on the first line.

My coverage reports look like:

foo/cistern/api/dao/dao.go:39:					NewTestResultRepo		100.0%
foo/cistern/api/dao/dao.go:46:					StudentsExistYN			85.7%
[etc]
total:								(statements)			93.8%

My test call looks like:

go test ./... -race -coverprofile cover.out && go tool cover -func cover.out -o /go/src/foo/cistern/coverage/backend.cov (this is running in a container with a mounted volume)

Is Sonar expecting an XML file here?

Sorted it out a few minutes later of course. You need to send the output of go test -coverprofile ... to Sonar, not the output of go tool cover.

1 Like

Hi, does this also apply to Automatic Analysis? Can I have Automatic Analysis on and Code Coverage for Go?

Hello @sebastianwr,

As stated in the documentation:

Current limitations:

  • Code coverage information is not supported.

In order to have code coverage, you will have to use the traditional analysis, you can have a look at this part of the documentation as a starting point.

Best,
Quentin

I’m trying to understand why I cannot get new lines of code or coverage to show up on PRs and branches for Golang

https://sonarcloud.io/dashboard?id=hofstadter-io_hof&pullRequest=63

Everything id public so you should be able see what I am doing

I have the same problem.In a branch it shows only the new lines and not the whole code plus everithing besides coverage estimated after merge is 0%.Did you solve it in anyway?