Code coverage ignore comments

Would like to see an option to add comments to exclude certain lines of code from code coverage like this...

println!("Loading...");

let response: ResponseType = match api_lib::get().await {
    Ok(response) => response,

    //coverage ignore start
    Err(err) => {
        eprintln!("{err}");
        std::process::exit(1);
    }
    //coverage ignore end
};

println!("Welcome...");

This will help get closer to 100% code coverage for those parts in your code where you do not expect to test such as weather a 3rd party API fails and weather the error is handled properly.

JavaScript's Vitest and PHP's PHPUnit has this feature in their own way.

imo Rust uses attributes for stuff like this rather than magic comments, e.g. the #[rustfmt::skip] attribute.

2 Likes

What do you currently use for code coverage?

If you use cargo-llvm-cov, the attribute is called coverage(off), like this:

cargo-tarpaulin also supports this attribute, and has its own attribute

This page talks more about the difference between those two tools

Anyway I'm not 100% sure but I think in both cases you apply to whole functions, not to sections of functions like this. I'm not sure if factoring this block into a function can help, either.

Anyway, since this doesn't require changes to Rust, I think further discussion should happen in https://users.rust-lang.org/

3 Likes