grcov has a bug in Windows, please run the command line with administrator
First of all, install grcov
cargo install grcov
Second, install the llvm-tools Rust component (llvm-tools-preview
for now, it might become llvm-tools
soon):
rustup component add llvm-tools-preview
# Export the flags needed to instrument the program to collect code coverage.
export RUSTFLAGS="-Zinstrument-coverage"
# Ensure each test runs gets its own profile information by defining the LLVM_PROFILE_FILE environment variable (%p will be replaced by the process ID, and %m by the binary signature):
export LLVM_PROFILE_FILE="your_name-%p-%m.profraw"
# Build the program
cargo build
# test the program
cargo test
# Generate a HTML report in the coverage/ directory.
grcov . --binary-path ./target/debug/ -s . -t html --branch --ignore-not-existing -o ./target/debug/coverage/
browser open the target/debug/coverage/index.html
Reference:
grcov