cargo test
The cargo test command is the unified test runner for Rust packages and workspaces, orchestrating the compilation and execution of unit tests, integration tests, binary tests, and documentation tests.
`cargo test` Target Selection:
cargo test
├── --lib (Unit tests in `src/lib.rs`)
├── --bins (Unit tests in `src/main.rs` & `src/bin/*.rs`)
├── --tests (All unit + integration tests, skipping doc-tests)
├── --test <name> (Specific integration test `tests/<name>.rs`)
└── --doc (Documentation tests in `///` comments)
Quick Reference of cargo test Flags
Remember that arguments before -- are interpreted by Cargo (controlling what gets compiled and which test binaries are invoked), whereas arguments after -- are passed directly to the compiled libtest harness binary:
| Command | Effect |
|---|---|
cargo test |
Compiles and runs all unit tests, integration tests, and doc-tests. |
cargo test --workspace |
Runs tests across all member packages in a workspace. |
cargo test --all-targets |
Runs unit, integration, binary, example, and bench targets in test mode (skips doc-tests). |
cargo test --no-run |
Compiles all test binaries without executing them (catches compile errors in tests). |
cargo test --no-fail-fast |
Runs all test binaries even if an earlier test binary fails. |
cargo test -- --show-output |
Displays println! output for passing tests after the run completes. |
cargo test -- --nocapture |
Streams println! output directly to the terminal without buffering. |
cargo test -- --test-threads=1 |
Runs tests sequentially on a single thread. |
cargo test -- --ignored |
Runs only tests annotated with #[ignore]. |
cargo test -- --include-ignored |
Runs both normal and #[ignore] tests. |
High-Performance Parallel Test Execution with cargo-nextest
While built-in cargo test executes all unit tests inside a single shared process (meaning a process-wide state mutation, segfault, or deadlock in one test can affect others) and runs separate integration test binaries one after another, the ecosystem tool cargo-nextest (cargo install cargo-nextest --locked) uses a process-per-test execution model:
| Feature | Built-In cargo test |
cargo nextest run |
|---|---|---|
| Execution Isolation | All tests in a binary share 1 OS process | Each test runs in its own isolated OS process |
| Cross-Binary Parallelism | Runs test binaries one at a time sequentially | Runs tests across all binaries concurrently in one pool |
| Slow Test & Hang Detection | Silent until completion | Reports SLOW warnings and terminates hung tests after timeout |
| Flaky Test Retries | Not built-in | Supports --retries N to detect and flag flaky tests |
| Doc-Tests | Included automatically | Run separately via cargo test --doc |
pub fn parse_key_value(line: &str) -> Option<(&str, &str)> {
let (k, v) = line.split_once('=')?;
Some((k.trim(), v.trim()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn splits_and_trims_pair() {
assert_eq!(parse_key_value(" host = 127.0.0.1 "), Some(("host", "127.0.0.1")));
}
}
fn main() {
println!("Parsed: {:?}", parse_key_value("port = 8080"));
}
Parsed: Some(("port", "8080"))