This project uses cargo-llvm-cov for code coverage tracking, integrated with Codecov for CI.
# Install cargo-llvm-cov
cargo install cargo-llvm-cov
# Add llvm-tools component
rustup component add llvm-tools-previewTerminal output (default):
cargo llvm-cov --all-features --workspaceHTML report (recommended):
cargo llvm-cov --all-features --workspace --html
# Opens target/llvm-cov/html/index.htmlUsing the helper script:
./scripts/coverage.sh
# Generates HTML and opens in browser automaticallycargo llvm-cov --all-features --workspaceShows per-file coverage with:
- Region coverage (branches)
- Function coverage
- Line coverage
Example output:
Filename Lines Missed Lines Cover
------------------------------------------------------------------------
domainstack/src/error.rs 182 0 100.00%
domainstack/src/rules/string.rs 123 0 100.00%
domainstack-derive/src/lib.rs 329 73 77.81%
------------------------------------------------------------------------
TOTAL 1358 78 94.26%
cargo llvm-cov --all-features --workspace --htmlCreates browsable HTML report at target/llvm-cov/html/index.html
Features:
- Click files to see line-by-line coverage
- Red lines = not covered
- Green lines = covered
- Filter by coverage percentage
- Search functionality
cargo llvm-cov --all-features --workspace --json --output-path coverage.jsoncargo llvm-cov --all-features --workspace --lcov --output-path lcov.infoThis is what CI uses to upload to Codecov.
Percentage of code lines executed by tests.
- Target: 80%+ for core library
- Good: Lines in error paths tested
- Bad: Large functions with no tests
Percentage of conditional branches tested.
- Example:
ifstatement with 2 branches (true/false) - Target: 70%+
- Important: Tests both success and error cases
Percentage of functions called by tests.
- Target: 90%+
- Easy win: Often 100% achievable
As of the latest run:
| Crate | Line Coverage | Status |
|---|---|---|
| domainstack | 98-100% | Excellent |
| domainstack-derive | 77% | |
| domainstack-envelope | 100% | Excellent |
| Overall | 94% | Great |
# Generate HTML report and look for red lines
cargo llvm-cov --all-features --workspace --html-
Error paths - Easy to miss in happy-path testing
// Make sure to test the Err case! if condition { Ok(value) } else { Err(error) // ← Test this path too }
-
Proc macros (domainstack-derive) - Hard to test
- Use trybuild for compile-fail tests
- Test generated code examples
-
Edge cases - Boundary conditions
// Test: empty, min, max, overflow validate_length(0, 255)
#[test]
fn test_error_path() {
let result = validate("", &rules::non_empty());
// Test the Err case
assert!(result.is_err());
// Also verify error details
let err = result.unwrap_err();
assert_eq!(err.violations[0].code, "non_empty");
}Coverage runs automatically on every push/PR:
- GitHub Actions runs tests with coverage
- Codecov receives and analyzes results
- PR comments show coverage changes
- Badge on README updates automatically
See codecov.yml for settings:
- Project target: 80%
- Patch target: 70%
- Ignore: examples, tests, benches
cargo install cargo-llvm-covrustup component add llvm-tools-preview# Clean and rebuild
cargo llvm-cov clean
cargo llvm-cov --all-features --workspacecargo llvm-cov --all-features -p domainstack --htmlcargo llvm-cov --all-features --workspace --show-missing-lines- Run coverage before committing - Catch regressions early
- Use HTML report - Much easier to spot gaps than terminal output
- Focus on critical paths - 100% coverage isn't always necessary
- Test error cases - Often the least covered code
- Ignore generated code - Configure in codecov.yml