Home

Code Coverage in Rust

Evergreen 🌳 - Published December 11, 2024

When you're writing tests it can be useful to know what in your codebase haven't been tested. Checking the code coverage will show you what individual lines haven't been tested and will let you go in and fill the gaps in your test suite.

With Rust you can do this using the LLVM code coverage tools.

There's a lot of info here in the rustc book. For the abridged version install the cargo llvm-cov helper.

cargo install cargo-llvm-cov

Once that installs, run your tests like so:

cargo llvm-cov --open

This will open your browser to an LLVM HTML coverage report showing the tree of files in your codebase. Click through to the files that you're currently testing, and you'll see the lines that are covered and the lines that are not:

coverage report showing lines covered and not covered in the tokio codebase


Bennett is a Software Engineer working at Foxglove. He spends most of his day playing with TypeScript and his nights programming in Rust. You can follow him on Github.
This work by Bennett Hardwick is licensed under CC BY-NC-SA 4.0Creative Commons CC logoCreative Commons BY logoCreative Commons NC logoCreative Commons SA logo.