Skip to content

Update code_coverage.md to include example for calculating coverage of entire Go project

  • Please check this box if this contribution uses AI-generated content (including content generated by GitLab Duo features) as outlined in the GitLab DCO & CLA

What does this MR do?

The current published version of this doc provides an example on how to integrate Go's test coverage tool. Due to the Gitlab CI/CD only capturing the last matching line for a given coverage regular expression in the job definition and the example command for Go resulting in the following output

ok      gitlab.com/example/foo 0.008s  coverage: 100.0% of statements
ok      gitlab.com/example/bar 0.008s  coverage: 100.0% of statements
ok      gitlab.com/example/fizz 0.008s  coverage: 95.0% of statements
ok      gitlab.com/example/buzz 0.008s  coverage: 80.0% of statements

Will result in the captured code coverage being 80.0% by Gitlab because the last line of the output reports the coverage for one of the packages in the project.

To have Gitlab capture the coverage for the entire project then the following two commands need to be executed:

  • go test -coverprofile=cover.profile -cover ./...
  • go tool cover -func cover.profile

The output of the go tool command will look like

gitlab.com/example/foo/foo.go:22:     Foo          100.0%
gitlab.com/example/bar/bar.go:11:     Bar          100.0%
gitlab.com/example/fizz/fizz.go:7:    Fizz         95.0%
gitlab.com/example/buzz/buzz.go:13:   Buzz         80.0%
total:                                (statements) 70.0%

So the following regular expression can be used on this new output to capture the project's total coverage /total:\s+(statements)\s+\d+.\d+%/ .

The changes in this MR would like to update the docs to provide the correct example for capturing the coverage of the entire project when using Go as the language for the project. For Gitlab let me know if you would like to special case the examples for Go as I have with the current revision between single package and whole project or would you just like to have one of the examples used instead for Go.

Related issues

Author's checklist

If you are a GitLab team member and only adding documentation, do not add any of the following labels:

  • ~"frontend"
  • ~"backend"
  • ~"type::bug"
  • ~"database"

These labels cause the MR to be added to code verification QA issues.

Reviewer's checklist

Documentation-related MRs should be reviewed by a Technical Writer for a non-blocking review, based on Documentation Guidelines and the Style Guide.

If you aren't sure which tech writer to ask, use roulette or ask in the #docs Slack channel.

  • If the content requires it, ensure the information is reviewed by a subject matter expert.
  • Technical writer review items:
    • Ensure docs metadata is present and up-to-date.
    • Ensure the appropriate labels are added to this MR.
    • Ensure a release milestone is set.
    • If relevant to this MR, ensure content topic type principles are in use, including:
      • The headings should be something you'd do a Google search for. Instead of Default behavior, say something like Default behavior when you close an issue.
      • The headings (other than the page title) should be active. Instead of Configuring GDK, say something like Configure GDK.
      • Any task steps should be written as a numbered list.
      • If the content still needs to be edited for topic types, you can create a follow-up issue with the docs-technical-debt label.
  • Review by assigned maintainer, who can always request/require the reviews above. Maintainer's review can occur before or after a technical writer review.

Merge request reports

Loading