Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can get coverage for cargo test?

When I want to test C++ coverage, I can build my program with -fprofile-arcs -ftest-coverage, run all tests, and run gcov to get coverages.

However, when it comes to Rust, I get totally lost. What I want to do is run the following tests(on my Mac), and get coverage of all Rust codes in path components/raftstore

cargo test --package tests --test failpoints cases::test_normal
cargo test --package tests --test failpoints cases::test_bootstrap
cargo test --package tests --test failpoints cases::test_compact_log

From this post, it says firstly run cargo test --no-run, then run kcov. However, when I actually do that, kcov blocks forever.

Then I find something called cargo kcov, who provides --test. However when I run cargo kcov --test failpoints cases::test_normal like what I do in cargo test, I get error

error: cargo subcommand failure
note: cargo test exited with code exit status: 101
error: no test target named `failpoints`

I have tried many ways to figure this out, however, none of them works, so I wonder if I can get some help here.

I know there are other coverage tools like tarpaulin and grcov, I am currently trying those. It is also acceptable if there are neat solutions with those coverage tools. However, I still want to know what is wrong with kcov and cargo-kcov.

like image 600
calvin Avatar asked Dec 04 '25 05:12

calvin


1 Answers

I like to use tarpaulin with the HTML output.

cargo tarpaulin --out Html

This should create an output file named tarpaulin-report.html. Open this file in the browser to view the code coverage report. 👍


Note: You'll need to have cargo-tarpaulin installed for this to work.

cargo install cargo-tarpaulin
like image 160
Jim Avatar answered Dec 05 '25 19:12

Jim