My company is turning on coverage reporting. We currently see the coverage % in our MRs and can see the lines covered in any code that changes. What we really want though is to get a holistic view of all the source code that is covered.
As far as I can tell the only way to get visualization of code that covered is to create an MR and change every file in your repo slightly and then click “Show whole file” on each file in an MR. This seems like a lot of work. What I’d really love is to be able to see the coverage as I click through the files in the Source view in the repo and be able to see the % of coverage in the file list views so I can drill into any file that has low coverage.
There are two ways I have found to do this, but both involve extra tooling.
Coverage HTML reports as artifacts
Use your coverage tool to generate an HTML report and list it as an artifact. You should then be able to click through from the CI job to the artifacts list and browse it directly without downloading and re-opening it from disk.
coverage-job-name:
variables:
COVERAGE_HTML_DIR: coverage_html
COVERAGE_COBERTURA_FILE: coverage.xml
script:
- cd build
# One run for Cobertura report (the coloured margin bars)
- >
gcovr
--root="$CI_PROJECT_DIR"
--xml --output="$COVERAGE_COBERTURA_FILE"
# Other grcov options...
.
# Another run to generate HTML coverage report
- >
gcovr
--root="$CI_PROJECT_DIR"
--html --output="$COVERAGE_HTML_DIR"
# Other grcov options...
.
# Extract and print percentage from Cobertura because Gitlab can't, see:
# https://gitlab.com/gitlab-org/gitlab/-/issues/21549.
- >
xmllint --xpath
"concat('gitlab-coverage ', 100 * string(//coverage/@line-rate), '%')"
"$COVERAGE_COBERTURA_FILE"
coverage: '/^gitlab-coverage .+%$/'
artifacts:
paths:
- "$PROJECT_NAME/build/$COVERAGE_HTML_DIR"
reports:
junit: $PROJECT_NAME/result.xml
coverage_report:
coverage_format: cobertura
path: $PROJECT_NAME/build/$COVERAGE_COBERTURA_FILE
I have omitted a bunch of config keys and explanation of paths etc. to emphasise the general concept, and to show how it might relate to other coverage processing. Grcov is not essential for this, any coverage tool that generates HTML should work here.
3rd party coverage analysis
Use a 3rd party/external coverage analysis service like Codecov, where you upload your coverage to them and they have a nice UI for browsing and analysing it.