Testing with cargo test, Doc Tests, Integration Tests
Intermediate12 min
Write and run unit tests, doc tests, and integration tests using Cargo's built-in testing framework.
Prerequisites
- -Rust toolchain installed
- -A Cargo project
Steps
1
Run all tests
Execute the full test suite including unit, doc, and integration tests.
$ cargo test
Use 'cargo test -- --nocapture' to see println! output from passing tests.
2
Run a specific test by name
Filter tests by name pattern to run only matching tests.
$ cargo test test_parse_config -- --exact
3
Create an integration test file
Add an integration test in the tests/ directory that tests your crate as an external consumer.
$ mkdir -p tests && cat > tests/integration_test.rs << 'EOF'
use myapp::process;
#[test]
fn test_full_pipeline() {
let result = process("input");
assert!(result.is_ok());
assert_eq!(result.unwrap(), "expected output");
}
EOF
4
Run tests with coverage using cargo-llvm-cov
Generate code coverage reports using the LLVM-based coverage tool.
$ cargo install cargo-llvm-cov && cargo llvm-cov --html
The HTML report is generated in target/llvm-cov/html/. Open index.html in your browser.
5
Run doc tests only
Execute only the documentation examples embedded in your code comments.
$ cargo test --doc
Full Script
FAQ
Discussion
Loading comments...