‹ back home

Measuring test coverage in Rust

2023-11-27 #how-to #rust

I want to measure test coverage for the vparser library, and this is my first time measuring coverage with Rust. Some notes for future reference.

First, run the tests with instrumentation enabled:

> RUSTFLAGS="-C instrument-coverage" cargo test -p vparser
   Compiling vparser v0.1.0 (/home/hugo/src/git.sr.ht/~whynothugo/vdirsyncer-rs/vparser)
    Finished test [unoptimized + debuginfo] target(s) in 0.33s
     Running unittests src/lib.rs (target/debug/deps/vparser-5204a1fc06fb7f74)
...

In some cases, you might need to merge multiple profraw files into a single one. This was not required in this particular case, but should be kept in mind if multiple files were generated.

llvm-profdata merge -sparse vparser/default_*.profraw -o vparser.profdata

Finally, show coverage with:

llvm-cov show --instr-profile vparser.profdata --object target/debug/deps/vparser-5204a1fc06fb7f74 --show-line-counts-or-regions

Note that the path target/debug/deps/vparser-5204a1fc06fb7f74 should match the one printed by cargo test above.

Both llvm-profdata and llvm-cov are provided by the llvm package.

It would be nice to find a plugin that reflects these results inside neovim (to quickly spot lines that are not covered).

Have comments or want to discuss this topic?
Send an email to ~whynothugo/public-inbox@lists.sr.ht (mailing list etiquette)

— § —