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 tests
org-integration-tests-directory - Place integration tests in tests directory
org-shared-test-utilities - Use tests/common/mod.rs for shared utilities
org-binary-crate-pattern - Extract logic from main.rs into lib.rs
org-test-naming - Name tests after behavior not implementation
org-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 traits
mock-automock-attribute - Use mockall automock for complex mocking
mock-avoid-mocking-owned-types - Avoid mocking types you own
mock-predicate-arguments - Use predicates to verify mock arguments
mock-returning-sequences - Use sequences for multiple return values
mock-static-methods - Use mock! macro for static methods
3. Async Testing (HIGH)
async-tokio-test-macro - Use tokio::test for async test functions
async-time-control - Use paused time for timeout testing
async-mock-io - Use tokio_test for mocking async IO
async-spawn-blocking - Test spawn_blocking with multi-threaded runtime
async-test-channels - Use channels for testing async communication
4. Property-Based Testing (HIGH)
prop-proptest-basics - Use proptest for property-based testing
prop-custom-strategies - Create custom strategies for domain types
prop-shrinking - Use shrinking to find minimal failing cases
prop-invariant-testing - Test invariants instead of specific values
5. Test Fixtures and Setup (MEDIUM)
fix-rstest-fixtures - Use rstest fixtures for test setup
fix-rstest-parametrized - Use rstest case for parameterized tests
fix-temp-directories - Use TempDir for file system tests
fix-test-context - Use test-context for setup and teardown
fix-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_err
assert-should-panic - Use should_panic for panic testing
assert-debug-display - Implement Debug for clear failure messages
assert-custom-messages - Add context to assertions with custom messages
assert-floating-point - Use approximate comparison for floating point
assert-collection-contents - Assert collection contents not just length
7. CI Integration (MEDIUM)
ci-cargo-nextest - Use cargo-nextest for faster CI
ci-caching - Cache Cargo dependencies in CI
ci-test-isolation - Ensure test isolation in parallel CI
ci-coverage - Generate coverage reports in CI
8. Test Performance (LOW-MEDIUM)
perf-compile-time - Reduce test compilation time
perf-test-filtering - Filter tests for faster feedback loops
perf-avoid-io-in-unit-tests - Avoid real IO in unit tests
perf-parallel-test-execution - Configure parallel test threads
perf-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
1---2name: rust-testing-23description: Rust Testing Best Practices4---5# Rust Testing Best Practices67Comprehensive 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.89## When to Apply1011Reference these guidelines when:12- Writing unit tests for Rust libraries or modules13- Creating integration tests for CLI applications14- Setting up mocking with mockall or trait-based design15- Testing async code with Tokio16- Configuring CI pipelines for Rust projects1718## Rule Categories by Priority1920| Priority | Category | Impact | Prefix |21|----------|----------|--------|--------|22| 1 | Test Organization | CRITICAL | `org-` |23| 2 | Mocking and Test Doubles | CRITICAL | `mock-` |24| 3 | Async Testing | HIGH | `async-` |25| 4 | Property-Based Testing | HIGH | `prop-` |26| 5 | Test Fixtures and Setup | MEDIUM | `fix-` |27| 6 | Assertions and Error Testing | MEDIUM | `assert-` |28| 7 | CI Integration | MEDIUM | `ci-` |29| 8 | Test Performance | LOW-MEDIUM | `perf-` |3031## Quick Reference3233### 1. Test Organization (CRITICAL)3435- [`org-unit-test-modules`](references/org-unit-test-modules.md) - Use cfg(test) modules for unit tests36- [`org-integration-tests-directory`](references/org-integration-tests-directory.md) - Place integration tests in tests directory37- [`org-shared-test-utilities`](references/org-shared-test-utilities.md) - Use tests/common/mod.rs for shared utilities38- [`org-binary-crate-pattern`](references/org-binary-crate-pattern.md) - Extract logic from main.rs into lib.rs39- [`org-test-naming`](references/org-test-naming.md) - Name tests after behavior not implementation40- [`org-test-cli-with-assert-cmd`](references/org-test-cli-with-assert-cmd.md) - Use assert_cmd for CLI testing4142### 2. Mocking and Test Doubles (CRITICAL)4344- [`mock-trait-based-design`](references/mock-trait-based-design.md) - Design for testability with traits45- [`mock-automock-attribute`](references/mock-automock-attribute.md) - Use mockall automock for complex mocking46- [`mock-avoid-mocking-owned-types`](references/mock-avoid-mocking-owned-types.md) - Avoid mocking types you own47- [`mock-expect-call-counts`](references/mock-expect-call-counts.md) - Verify mock call counts explicitly48- [`mock-predicate-arguments`](references/mock-predicate-arguments.md) - Use predicates to verify mock arguments49- [`mock-returning-sequences`](references/mock-returning-sequences.md) - Use sequences for multiple return values50- [`mock-static-methods`](references/mock-static-methods.md) - Use mock! macro for static methods5152### 3. Async Testing (HIGH)5354- [`async-tokio-test-macro`](references/async-tokio-test-macro.md) - Use tokio::test for async test functions55- [`async-time-control`](references/async-time-control.md) - Use paused time for timeout testing56- [`async-mock-io`](references/async-mock-io.md) - Use tokio_test for mocking async IO57- [`async-spawn-blocking`](references/async-spawn-blocking.md) - Test spawn_blocking with multi-threaded runtime58- [`async-test-channels`](references/async-test-channels.md) - Use channels for testing async communication5960### 4. Property-Based Testing (HIGH)6162- [`prop-proptest-basics`](references/prop-proptest-basics.md) - Use proptest for property-based testing63- [`prop-custom-strategies`](references/prop-custom-strategies.md) - Create custom strategies for domain types64- [`prop-shrinking`](references/prop-shrinking.md) - Use shrinking to find minimal failing cases65- [`prop-invariant-testing`](references/prop-invariant-testing.md) - Test invariants instead of specific values6667### 5. Test Fixtures and Setup (MEDIUM)6869- [`fix-rstest-fixtures`](references/fix-rstest-fixtures.md) - Use rstest fixtures for test setup70- [`fix-rstest-parametrized`](references/fix-rstest-parametrized.md) - Use rstest case for parameterized tests71- [`fix-temp-directories`](references/fix-temp-directories.md) - Use TempDir for file system tests72- [`fix-test-context`](references/fix-test-context.md) - Use test-context for setup and teardown73- [`fix-once-cell-shared-state`](references/fix-once-cell-shared-state.md) - Use OnceCell for expensive shared setup7475### 6. Assertions and Error Testing (MEDIUM)7677- [`assert-specific-errors`](references/assert-specific-errors.md) - Assert specific error types not just is_err78- [`assert-should-panic`](references/assert-should-panic.md) - Use should_panic for panic testing79- [`assert-debug-display`](references/assert-debug-display.md) - Implement Debug for clear failure messages80- [`assert-custom-messages`](references/assert-custom-messages.md) - Add context to assertions with custom messages81- [`assert-floating-point`](references/assert-floating-point.md) - Use approximate comparison for floating point82- [`assert-collection-contents`](references/assert-collection-contents.md) - Assert collection contents not just length8384### 7. CI Integration (MEDIUM)8586- [`ci-cargo-nextest`](references/ci-cargo-nextest.md) - Use cargo-nextest for faster CI87- [`ci-caching`](references/ci-caching.md) - Cache Cargo dependencies in CI88- [`ci-test-isolation`](references/ci-test-isolation.md) - Ensure test isolation in parallel CI89- [`ci-coverage`](references/ci-coverage.md) - Generate coverage reports in CI9091### 8. Test Performance (LOW-MEDIUM)9293- [`perf-compile-time`](references/perf-compile-time.md) - Reduce test compilation time94- [`perf-test-filtering`](references/perf-test-filtering.md) - Filter tests for faster feedback loops95- [`perf-avoid-io-in-unit-tests`](references/perf-avoid-io-in-unit-tests.md) - Avoid real IO in unit tests96- [`perf-parallel-test-execution`](references/perf-parallel-test-execution.md) - Configure parallel test threads97- [`perf-benchmark-critical-paths`](references/perf-benchmark-critical-paths.md) - Benchmark critical paths with Criterion9899## How to Use100101Read individual reference files for detailed explanations and code examples:102103- [Section definitions](references/_sections.md) - Category structure and impact levels104- [Rule template](assets/templates/_template.md) - Template for adding new rules105106## Reference Files107108| File | Description |109|------|-------------|110| [references/_sections.md](references/_sections.md) | Category definitions and ordering |111| [assets/templates/_template.md](assets/templates/_template.md) | Template for new rules |112| [metadata.json](metadata.json) | Version and reference information |
Run npx skillmds@latest add comeonoliver/rust-testing-2 in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Rust Testing Best Practices It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. Capability flags: docs only. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
ComeOnOliver (@comeonoliver) published this skill. Their other Agent Skills are listed on their SkillMD profile.