# generate cobertura report for gitlab integration Passing this report to GitLab as a reports artifact will enable Test Coverage Visualization allowing reviewers to easily check if a MR is changing tested or untested code. To do so we need to feed it a cobertura report which is currently not supported by grcov.įortunately, it's possible to generate a lcov report and then convert it to cobertura as suggested in the ticket. The html report we just generated is very handy for manually checking which part of the code is covered by tests but is not usable directly by GitLab. I'd suggest to first generate a report without any -ignore and then tweak it to exclude all the code not from your crate. Depending of your exact setup you may have to adjust it. This is achieved by passing the -ignore "*cargo*" flag to grcov to exclude code from the cargo registry. We want our coverage report to cover only the code of our current crate, not its dependencies. t html -branch -ignore-not-existing -ignore "*cargo*" -o. Here is an example of such report for the zbus crate. We can then use it to generate a html report that we'll export as a job artifact. This is done using grcov from Mozilla which is installed using cargo install.
Now that the Rust compiler has generated the coverage profiles we can generate a report. rustup component add llvm-tools-preview
LLVM_PROFILE_FILE: "coverage-%p-%m.profraw" Here is how a coverage CI job would look like: coverage: Once we have setup these variables we just need to run the tests as usual. To avoid this, we'll also define LLVM_PROFILE_FILE with a generic pattern so each test will save its profile to its own file: LLVM_PROFILE_FILE="coverage-%p-%m.profraw".
This will be a problem if we have multiple tests as each one will override the profile of the previous one. By default, the profile is saved to a file called default.profraw. In order to generate the code coverage information, called raw profile, we need to set the environment variable RUSTFLAGS="-Zinstrument-coverage". It also needs the llvm-tools-preview component. LLVM source-base code coverage instrumentation is currently only available in Rust nightly so our job will use an image providing this version of the compiler. See the GitLab documentation if your project does not have any CI setup yet. The frst step is to add a new job to your CI pipeline, which will take care of generating the coverage reports.
#COLLABORA CODE GITHUB HOW TO#
In this post we'll explain how to setup a CI job in a Rust project to feed source-base coverage information to GitLab. GitLab provides built-in integration of coverage information allowing for example reviewers to check if a MR is changing tested code or if it's increasing or decreasing the total coverage of the project. This feature is called source-base because it operates on AST and preprocessor information directly, producing more precise coverage data compared to the traditional gcov coverage technique. Earlier this year, the Rust compiler gained support for LLVM source-base code coverage.