Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).
Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).
Cargo subcommand to easily use LLVM source-based code coverage.
This is a wrapper around rustc [-C instrument-coverage][instrument-coverage] and provides:
cargo test, cargo run, and [cargo nextest][nextest] with command-line interface compatible with cargo.Table of Contents:
(See docs directory for options of subcommands)
…By default, run tests (via cargo test), and print the coverage summary to stdout.
cargo llvm-covCurrently, doc tests are disabled by default because nightly-only features are required to make coverage work for doc tests. see [#2] for more.
To run cargo run instead of cargo test, use run subcommand.
cargo llvm-cov runWith html report (the report will be generated to target/llvm-cov/html directory):
cargo llvm-cov --html
open target/llvm-cov/html/index.htmlor
cargo llvm-cov --openWith plain text report (if --output-path is not specified, the report will be printed to stdout):
cargo llvm-cov --text | less -RWith json report (if --output-path is not specified, the report will be printed to stdout):
cargo llvm-cov --json --output-path cov.jsonWith lcov report (if --output-path is not specified, the report will be printed to stdout):
cargo llvm-cov --lcov --output-path lcov.infoYou can get a coverage report in a different format based on the results of a previous run by using cargo llvm-cov report.
cargo llvm-cov --html # run tests and generate html report
cargo llvm-cov report --lcov # generate lcov reportcargo llvm-cov/cargo llvm-cov run/cargo llvm-cov nextest cleans some build artifacts by default to avoid false positives/false negatives due to old build artifacts.
This behavior is disabled when --no-clean, --no-report, or --no-run is passed, and old build artifacts are retained.
When using these flags, it is recommended to first run cargo llvm-cov clean --workspace to remove artifacts that may affect the coverage results.
cargo llvm-cov clean --workspace # remove artifacts that may affect the coverage results
cargo llvm-cov --no-cleanYou can merge the coverages generated under different test conditions by using --no-report and cargo llvm-cov report.
cargo llvm-cov clean --workspace # remove artifacts that may affect the coverage results
cargo llvm-cov --no-report --features a
cargo llvm-cov --no-report --features b
cargo llvm-cov report --lcov # generate report without tests[!TIP] To include coverage for doctests you also need to pass
--docteststocargo llvm-cov report.
Set CC, CXX, LLVM_COV, and LLVM_PROFDATA environment variables to Clang/LLVM compatible with the LLVM version used in rustc, and run cargo-llvm-cov with --include-ffi flag.
CC=<clang-path> \
CXX=<clang++-path> \
LLVM_COV=<llvm-cov-path> \
LLVM_PROFDATA=<llvm-profdata-path> \
cargo llvm-cov --lcov --include-ffiKnown compatible Rust (installed via rustup) and LLVM versions:
| Rust 1.60-1.77 | Rust 1.78-1.81 | Rust 1.82-1.98 | Rust 1.99-1.100 | |
|---|---|---|---|---|
| LLVM 14-17 | ✓ | |||
| LLVM 18 | ✓ | |||
| LLVM 19-22 | ✓ | |||
| LLVM 23 | ✓ |
cargo test, cargo run, and [cargo nextest][nextest] are available as builtin, but cargo-llvm-cov can also be used for arbitrary binaries built using cargo (including other cargo subcommands or external tests that use make, [xtask], etc.)
# Set the environment variables needed to get coverage.
source <(cargo llvm-cov show-env --sh)
# Remove artifacts that may affect the coverage results.
# This command should be called after show-env.
cargo llvm-cov clean --workspace
# Above two commands should be called before build binaries.
cargo build # Build rust binaries with instrumentation.
# Commands using binaries in target/debug/*, including `cargo test` and other cargo subcommands.
# ...
cargo llvm-cov report --lcov # Generate report without tests.[!CAUTION] cargo-llvm-cov subcommands other than
reportandcleanmay not work correctly in the context where environment variables are set byshow-env; consider using normalcargo/cargo-nextest/etc. commands.
[!TIP] To include coverage for doctests you also need to pass
--docteststo bothcargo llvm-cov show-envandcargo llvm-cov report.
[!TIP] The same thing can be achieved in PowerShell 6+ by substituting the source command with:
Invoke-Expression (cargo llvm-cov show-env --pwsh | Out-String)See "Test show-env ..." in our CI config for usage with other shells (cmd.exe, csh, tcsh, fish, nushell, xonsh).
cargo-llvm-cov can be used with AFL.rs similar to the way external tests are done, but with a few caveats.
…To exclude specific file patterns from the report, use the --ignore-filename-regex option.
cargo llvm-cov --open --ignore-filename-regex buildBy default, vendored sources will not be included in the report. Also, the following patterns and crates specified by --exclude or --exclude-from-report are excluded:
{SEPARATOR}rustc{SEPARATOR}([0-9a-f]+|[0-9]+\.[0-9]+\.[0-9]+){SEPARATOR}
^{WORKSPACE_ROOT}({SEPARATOR}.*)?{SEPARATOR}(tests|examples|benches){SEPARATOR}
^{WORKSPACE_ROOT}({SEPARATOR}.*)?{SEPARATOR}(tests.rs|[0-9a-zA-Z_-]+[_-]tests.rs)$
^{TARGET_DIR}($|{SEPARATOR})
^{CARGO_HOME}{SEPARATOR}(registry|git){SEPARATOR}
^{RUSTUP_HOME}{SEPARATOR}toolchains($|{SEPARATOR})You can use --no-default-ignore-filename-regex to disable default exclude setting.
To exclude the specific function or module from coverage, use the [#[coverage(off)] attribute][rust-lang/rust#84605].
Since #[coverage(off)] is unstable, it is recommended to use it together with cfg(coverage) or cfg(coverage_nightly) set by cargo-llvm-cov.
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
// function
#[cfg_attr(coverage_nightly, coverage(off))]
fn exclude_fn_from_coverage() {
// ...
}
// module
#[cfg_attr(coverage_nightly, coverage(off))]
mod exclude_mod_from_coverage {
// ...
}cfgs are set under the following conditions:
cfg(coverage) is always set when using cargo-llvm-cov (unless --no-cfg-coverage flag passed)cfg(coverage_nightly) is set when using cargo-llvm-cov with nightly toolchain (unless --no-cfg-coverage-nightly flag passed)Rust 1.80+ warns the above cfgs as unexpected_cfgs. The recommended way to address this is to add a lints table to Cargo.toml.
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage,coverage_nightly)'] }If you want to ignore all #[test]-related code, you can use module-level #[coverage(off)] attribute:
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
// ...
}cargo-llvm-cov excludes code contained in the directory named tests and file named tests.rs/*_tests.rs/*-tests.rs from the report by default, so you can also use it instead of #[coverage(off)] attribute.
{x86_64,i686}-pc-windows-msvc and {x86_64,aarch64}-pc-windows-gnullvm are confirmed to work.
As of 2025-12-30, x86_64-pc-windows-gnu (rust-lang/rust#111098) and aarch64-pc-windows-msvc (rust-lang/rust#150123) are known to not work with default setup.
See the wasm-bindgen guide for the way to get coverage for wasm32-unknown-unknown target.
Here is an example of GitHub Actions workflow that uploads coverage to [Codecov].
…Currently, when using --lcov flag, [only line coverage is available on Codecov][#20].
By using --codecov flag instead of --lcov flag, you can use region coverage on Codecov:
- name: Generate code coverage
run: cargo llvm-cov --all-features --workspace --codecov --output-path codecov.json
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }} # required for private repos or protected branches
files: codecov.json
fail_ci_if_error: trueNote that the way Codecov shows region/branch coverage is not very good.
First of all, when running the CI you need to make sure cargo-llvm-cov is available
in the execution script. Whether you add it to a custom image, or run cargo install as part
of your pipeline, it should be available and in PATH. Once done, it's simple:
unit_tests:
artifacts:
reports:
junit: target/nextest/default/junit.xml
coverage_report:
coverage_format: cobertura
path: target/llvm-cov-target/cobertura.xml
# this uses region for coverage summary
coverage: '/TOTAL\s+(\d+\s+)+(\d+\.\d+\%)/'
script:
- cargo llvm-cov nextest
- cargo llvm-cov report --cobertura --output-path target/llvm-cov-target/cobertura.xml[!CAUTION] GitLab has certain [limits for Cobertura reports](https://docs.gitlab.com/ci/testing
No open issues yet, or sync has not completed.