Testing Best Practices
Fast, deterministic, meaningful tests give Rust teams confidence to refactor without flaking CI or duplicating production incidents.
Busca en todas las páginas de la documentación
Fast, deterministic, meaningful tests give Rust teams confidence to refactor without flaking CI or duplicating production incidents.
#[cfg(test)] mod tests. Private API tested beside implementation.tests/ per behavior area. One file per feature, not one megaf ile.main, fat lib.tests/common/. No copy-paste HTTP/DB setup.cargo test. Mocks, fakes, or testcontainers behind #[ignore].tokio::time::pause instead of sleep in async tests. Faster, deterministic timing.serial_test. Parallel tests must not share mut statics.proptest-regressions. Reproducible failures.mockall or manual fakes (Mocking & Test Doubles).scenario_expected_outcome. Readable cargo test filter names.cargo nextest run --workspace in CI. Faster than plain test on large repos.clippy -D warnings before merge. Tests pass in same profile as developers.#[tokio::test] with correct flavor. current_thread only when !Send required.ServiceExt::oneshot. No random ports in unit layer.#[sqlx::test] or rolled-back transactions for DB. No leftover rows between tests.DATABASE_URL). README for local E2E.One happy path integration, one auth/validation failure, unit tests on extracted domain logic.
A few strong invariants per parser/serializer; not every function needs proptest.
Fix or quarantine with owner and deadline; never ignore indefinitely on main.
Yes via unit tests in same module; prefer testing via public API when behavior is stable contract.
Mock/fake for speed; testcontainers for SQL dialect quirks in nightly job.
cargo test --doc --workspace in PR if docs are part of the API contract.
assert_cmd or snapshot of --help; integration with temp dirs for file output.
When behavior removed; do not keep tests for deleted features "just in case."
Lower bar acceptable for thin wiring; high bar on domain and security modules.
Team rotation; tie flaky test fixes to same sprint as feature work touching that area.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 16 jul 2026