Collect test coverage on all the test cases for the current project using
kcov on Linux and macOS.
In the project run
sh
$ cargo kcov
It will run all test cases and collect coverage statistics via kcov. The coverage report can be read
from target/cov/index.html.
You need to install kcov v26 or above to collect coverage report from Rust. Some distro is still
shipping v25 or v11, so you will need to build from source.
For macOS, you will need kcov v35 or above. Be aware that macOS performance is significantly
slower when compared with Linux. If you are collecting coverage statistics on the CI and you don't
have time to spend, consider ignoring macOS.
Please follow the instruction in https://users.rust-lang.org/t/650. cargo-kcov requires v30 or
above since earlier versions of kcov do not report its version number.
cargo-kcov requires Rust 1.20.0 or above (due to bitflags dependency).
cargo-kcov can be installed with cargo install.
sh
$ cargo install cargo-kcov
```text cargo-kcov 0.5.1 Generate coverage report via kcov
USAGE: cargo kcov [OPTIONS] [--] [KCOV-ARGS]...
OPTIONS:
--lib Test only this package's library
--bin default feature
--target target/
folder is used exclusively by one rustc/cargo version only, and the test cases are
built with RUSTFLAGS="-C link-dead-code" cargo test.
--print-install-kcov-sh Prints the sh code that installs kcov to ~/.cargo/bin. Note that this will not
install dependencies required by kcov.
-h, --help Prints help information
-V, --version Prints version information
ARGS:
--verify --exclude-
pattern=$CARGO_HOME will be passed to kcov.
```