Rust Testing Best Practices
Comprehensive testing guide for Rust applications, covering CLI testing, library testing, async patterns, and CI integration. Contains 42 rules across 8 categories, prioritized by impact to guide test design, mocking strategies, and CI optimization.
When to Apply
Reference these guidelines when:
- Writing unit tests for Rust libraries or modules
- Creating integration tests for CLI applications
- Setting up mocking with mockall or trait-based design
- Testing async code with Tokio
- Configuring CI pipelines for Rust projects
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|----------|----------|--------|--------|
| 1 | Test Organization | CRITICAL | org- |
| 2 | Mocking and Test Doubles | CRITICAL | mock- |
| 3 | Async Testing | HIGH | async- |
| 4 | Property-Based Testing | HIGH | prop- |
| 5 | Test Fixtures and Setup | MEDIUM | fix- |
| 6 | Assertions and Error Testing | MEDIUM | assert- |
| 7 | CI Integration | MEDIUM | ci- |
| 8 | Test Performance | LOW-MEDIUM | perf- |
Quick Reference
1. Test Organization (CRITICAL)
org-unit-test-modules- Use cfg(test) modules for unit testsorg-integration-tests-directory- Place integration tests in tests directoryorg-shared-test-utilities- Use tests/common/mod.rs for shared utilitiesorg-binary-crate-pattern- Extract logic from main.rs into lib.rsorg-test-naming- Name tests after behavior not implementationorg-test-cli-with-assert-cmd- Use assert_cmd for CLI testing
2. Mocking and Test Doubles (CRITICAL)
mock-trait-based-design- Design for testability with traitsmock-automock-attribute- Use mockall automock for complex mockingmock-avoid-mocking-owned-types- Avoid mocking types you ownmock-expect-call-counts- Verify mock call counts explicitlymock-predicate-arguments- Use predicates to verify mock argumentsmock-returning-sequences- Use sequences for multiple return valuesmock-static-methods- Use mock! macro for static methods
3. Async Testing (HIGH)
async-tokio-test-macro- Use tokio::test for async test functionsasync-time-control- Use paused time for timeout testingasync-mock-io- Use tokio_test for mocking async IOasync-spawn-blocking- Test spawn_blocking with multi-threaded runtimeasync-test-channels- Use channels for testing async communication
4. Property-Based Testing (HIGH)
prop-proptest-basics- Use proptest for property-based testingprop-custom-strategies- Create custom strategies for domain typesprop-shrinking- Use shrinking to find minimal failing casesprop-invariant-testing- Test invariants instead of specific values
5. Test Fixtures and Setup (MEDIUM)
fix-rstest-fixtures- Use rstest fixtures for test setupfix-rstest-parametrized- Use rstest case for parameterized testsfix-temp-directories- Use TempDir for file system testsfix-test-context- Use test-context for setup and teardownfix-once-cell-shared-state- Use OnceCell for expensive shared setup
6. Assertions and Error Testing (MEDIUM)
assert-specific-errors- Assert specific error types not just is_errassert-should-panic- Use should_panic for panic testingassert-debug-display- Implement Debug for clear failure messagesassert-custom-messages- Add context to assertions with custom messagesassert-floating-point- Use approximate comparison for floating pointassert-collection-contents- Assert collection contents not just length
7. CI Integration (MEDIUM)
ci-cargo-nextest- Use cargo-nextest for faster CIci-caching- Cache Cargo dependencies in CIci-test-isolation- Ensure test isolation in parallel CIci-coverage- Generate coverage reports in CI
8. Test Performance (LOW-MEDIUM)
perf-compile-time- Reduce test compilation timeperf-test-filtering- Filter tests for faster feedback loopsperf-avoid-io-in-unit-tests- Avoid real IO in unit testsperf-parallel-test-execution- Configure parallel test threadsperf-benchmark-critical-paths- Benchmark critical paths with Criterion
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
Reference Files
| File | Description | |------|-------------| | references/_sections.md | Category definitions and ordering | | assets/templates/_template.md | Template for new rules | | metadata.json | Version and reference information |