I am trying to get unit test coverage reporting for a Go project. At the moment I am not seeing any coverage details reported in the SonarQube server. I did get this working for a Go project several months ago but that one was not Go Modules based.
Which versions are you using?
13:37:39.616 INFO: SonarQube Scanner 4.0.0.1744
13:37:39.617 INFO: Java 11.0.1 Oracle Corporation (64-bit)
13:37:39.617 INFO: Linux 4.15.0-106-generic amd64
13:37:40.999 INFO: SonarQube server 8.1.0
What are you trying to achieve?
Add Test Coverage reporting to a Go / Golang project. I seem to have managed to add the Test results as I get issues about some Unit Tests not being run. I do not get any coverage reporting on the feature brach underwhich I am trying to add the unit test coder coverage reporting.
What have you tried so far to achieve this?
I have setup my properties file:
# Turn on debugging while working out when Coverage is not shown
sonar.verbose=true
# The name and description displayed in the SonarQube UI
sonar.projectName=ProjName
sonar.projectDescription=A short description
# Project Link Data
sonar.links.homepage=https://...
sonar.links.ci=https://...
sonar.links.issue=...
sonar.links.scm=https://...
# General Project Settings
sonar.coverage.exclusions=./test/**
sonar.exclusions=**/opencv/*.cpp,**/opencv/*.h
sonar.tests=.
sonar.test.exclusions=**/*integration_test.go,**/vendor/**
sonar.test.inclusions=**/*_test.go
sonar.sources=.
# C, C++ & ObjC Settings
# Block C, C++ and Objective-C Analysis to avoid confusing the Python Analysis
sonar.c.file.suffixes=-
sonar.cpp.file.suffixes=-
sonar.objc.file.suffixes=-
# Go Settings
sonar.go.coverage.reportPaths=cover.cov
#sonar.go.golint.reportPaths=golint-report.out
#sonar.go.govet.reportPaths=govet-report.out
sonar.go.tests.reportPaths=unit-test-results.json
I have generated a cover.cov report using: go test -v -count=1 -json .../. | tee unit-test-results.json
It contains lists similar to:
{"Time":"2020-06-25T15:30:31.682179872+02:00","Action":"run","Package":"example.company.com/some/url/stuff/project.git/fileutils","Test":"TestDirectoryCreation"}
{"Time":"2020-06-25T15:30:31.68353597+02:00","Action":"output","Package":"example.company.com/some/url/stuff/project.git/fileutils","Test":"TestDirectoryCreation","Output":"=== RUN TestDirectoryCreation\n"}
{"Time":"2020-06-25T15:30:31.690894599+02:00","Action":"output","Package":"example.company.com/some/url/stuff/project.git/fileutils","Test":"TestDirectoryCreation","Output":"--- PASS: TestDirectoryCreation (0.00s)\n"}
{"Time":"2020-06-25T15:30:31.69224484+02:00","Action":"pass","Package":"example.company.com/some/url/stuff/project.git/fileutils","Test":"TestDirectoryCreation","Elapsed":0}
{"Time":"2020-06-25T15:30:31.692372686+02:00","Action":"run","Package":"example.company.com/some/url/stuff/project.git/fileutils","Test":"TestFileCreation"}
...
I have generated a unit-test-results.json report using: `go test -covermode=count -coverprofile=“cover.cov” “./…”
It contains lists similar to:
mode: count
example.company.com/some/url/stuff/project.git/fileutils/fileutils.go:15.68,16.49 1 3
example.company.com/some/url/stuff/project.git/fileutils/fileutils.go:16.49,18.17 2 3
example.company.com/some/url/stuff/project.git/fileutils/fileutils.go:18.17,20.4 1 0
example.company.com/some/url/stuff/project.git/fileutils/fileutils.go:24.96,30.2 4 2
My directory structure goes several layers deep in places but you get the idea. I am using Go Modules.
The possibly related parts from the scanner output look like:
13:37:44.752 DEBUG: * SonarGo 1.6.0.719 (go)
...
13:37:46.478 INFO: Indexing files...
13:37:46.479 INFO: Project configuration:
13:37:46.480 INFO: Excluded sources: **/opencv/*.cpp, **/opencv/*.h, **/*_test.go
13:37:46.480 INFO: Included tests: **/*_test.go
13:37:46.480 INFO: Excluded tests: **/*integration_test.go, **/vendor/**
13:37:46.481 INFO: Excluded sources for coverage: **/**
13:37:46.502 DEBUG: readpipe [git, --version],/usr/bin
13:37:46.518 DEBUG: readpipe may return 'git version 2.19.1'
13:37:46.519 DEBUG: remaining output:
13:37:46.519 DEBUG: readpipe [git, config, --system, --edit],/usr/bin
13:37:46.527 DEBUG: readpipe may return '/etc/gitconfig'
13:37:46.527 DEBUG: remaining output:
13:37:46.637 DEBUG: 487 non excluded files in this Git repository
13:37:46.651 DEBUG: 'tools/prebuild.go' indexed with language 'go'
13:37:46.652 DEBUG: File tools/prebuild.go excluded for coverage
13:37:46.655 DEBUG: 'tools/api/logger/test_resources/anEvent.json' indexed with language 'null'
13:37:46.655 DEBUG: File tools/api/logger/test_resources/anEvent.json excluded for coverage
...
13:37:47.130 DEBUG: 'modules/event/event_adapter_test.go' indexed as test with language 'go'
13:37:47.130 DEBUG: File modules/event/event_adapter_test.go excluded for coverage
13:37:47.131 DEBUG: 'log/logger_utils_test.go' indexed as test with language 'go'
13:37:47.131 DEBUG: File log/logger_utils_test.go excluded for coverage
13:37:47.132 INFO: 467 files indexed
13:37:47.132 INFO: 0 files ignored because of inclusion/exclusion patterns
13:37:47.132 INFO: 0 files ignored because of scm ignore settings
13:37:47.133 INFO: Quality profile for go: Frauscher Go way
13:37:47.133 INFO: Quality profile for py: Frauscher PEP-008 Python Way
13:37:47.133 INFO: ------------- Run sensors on module Sensonyx
13:37:47.271 INFO: Load metrics repository
...
13:37:49.762 DEBUG: 'Import of Bandit issues' skipped because one of the required properties is missing
13:37:49.763 DEBUG: 'Import of go vet issues' skipped because one of the required properties is missing
13:37:49.763 DEBUG: 'Import of Golint issues' skipped because one of the required properties is missing
13:37:49.763 DEBUG: 'Import of GoMetaLinter issues' skipped because one of the required properties is missing
13:37:49.764 DEBUG: 'Import of GolangCI-Lint issues' skipped because one of the required properties is missing
13:37:49.764 DEBUG: 'Kotlin Sensor' skipped because there is no related file in current project
...
13:37:50.641 INFO: Sensor SonarGo [go]
13:37:50.643 INFO: 199 source files to be analyzed
13:37:50.703 DEBUG: 'test/testsupport/server_testcontainer.go' generated metadata with charset 'UTF-8'
13:37:50.813 DEBUG: 'modules/tracking/detector/detector_module.go' generated metadata with charset 'UTF-8'
13:37:50.854 DEBUG: 'modules/tracking/detector/recorder_factory.go' generated metadata with charset 'UTF-8'
...
13:37:54.276 WARN: Failed to find test file for package example.company.com/some/url/stuff/project.git/modules/monitoring/changedetection and test Test_Integration_Detection
13:37:54.315 INFO: Sensor Go Unit Test Report [go] (done) | time=285ms
13:37:54.315 INFO: Sensor Go Cover sensor for Go coverage [go]
13:37:54.316 INFO: Load coverage report from '/builds/rnd/group/projectname/cover.cov'
13:37:54.420 DEBUG: Resolving file example.company.com/some/url/stuff/project.git/tools/api/controller/event_rate_controller_mock.go using relative path
13:37:54.421 DEBUG: Saving coverage measures for file 'example.company.com/some/url/stuff/project.git/tools/api/controller/event_rate_controller_mock.go'
...
13:37:54.500 DEBUG: Resolving file example.company.com/some/url/stuff/project.git/modules/tracking/edgedetector/edge_detector.go using relative path
13:37:54.500 DEBUG: Saving coverage measures for file 'example.company.com/some/url/stuff/project.git/modules/tracking/edgedetector/edge_detector.go'
13:37:54.501 INFO: Sensor Go Cover sensor for Go coverage [go] (done) | time=186ms
13:37:54.501 INFO: Sensor JavaSecuritySensor [security]
...
13:37:54.857 DEBUG: Detection of duplications for /builds/rnd/group/projectname/modules/monitoring/changedetection/layout_manager.go
13:37:54.866 DEBUG: Detection of duplications for /builds/rnd/group/projectname/tools/recorder/recoder.go
...