Test coverage in GitLab CI in a Golang project

GitLab.com offers free git repositories and similarly to GitHub, free CI which comes by default in your repository. GitLabCI can automatically generate badges for your project like build status and test coverage.

To start using GitLabCI without a hassle, GitLab repository manager offers .gitlabci.yml templates for your projects. For a Golang project, edit the default template, find the line that starts with go test -race and append -coverprofile .testCoverage.txt, so it looks like the one below:

- go test -race $(go list ./... | grep -v /vendor/) -v -coverprofile .testCoverage.txt

Go to Settings -> CI / CD, expand section General pipelines settings and add this regular expression  ^coverage:\s(\d+(?:\.\d+)?%) to Test coverage parsing

Next time you run your pipelines, test stage will contain something like:

PASS
coverage: 83.9% of statements

It will generate badges with build status and test coverage:

pipeline status coverage report