install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/pproenca/dot-skills/rust-testing" ~/.claude/skills/comeonoliver-skillshub-rust-testing-7e2ebb && rm -rf "$T"
manifest:
skills/pproenca/dot-skills/rust-testing/SKILL.mdsource content
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 | |
| 2 | Mocking and Test Doubles | CRITICAL | |
| 3 | Async Testing | HIGH | |
| 4 | Property-Based Testing | HIGH | |
| 5 | Test Fixtures and Setup | MEDIUM | |
| 6 | Assertions and Error Testing | MEDIUM | |
| 7 | CI Integration | MEDIUM | |
| 8 | Test Performance | LOW-MEDIUM | |
Quick Reference
1. Test Organization (CRITICAL)
- Use cfg(test) modules for unit testsorg-unit-test-modules
- Place integration tests in tests directoryorg-integration-tests-directory
- Use tests/common/mod.rs for shared utilitiesorg-shared-test-utilities
- Extract logic from main.rs into lib.rsorg-binary-crate-pattern
- Name tests after behavior not implementationorg-test-naming
- Use assert_cmd for CLI testingorg-test-cli-with-assert-cmd
2. Mocking and Test Doubles (CRITICAL)
- Design for testability with traitsmock-trait-based-design
- Use mockall automock for complex mockingmock-automock-attribute
- Avoid mocking types you ownmock-avoid-mocking-owned-types
- Verify mock call counts explicitlymock-expect-call-counts
- Use predicates to verify mock argumentsmock-predicate-arguments
- Use sequences for multiple return valuesmock-returning-sequences
- Use mock! macro for static methodsmock-static-methods
3. Async Testing (HIGH)
- Use tokio::test for async test functionsasync-tokio-test-macro
- Use paused time for timeout testingasync-time-control
- Use tokio_test for mocking async IOasync-mock-io
- Test spawn_blocking with multi-threaded runtimeasync-spawn-blocking
- Use channels for testing async communicationasync-test-channels
4. Property-Based Testing (HIGH)
- Use proptest for property-based testingprop-proptest-basics
- Create custom strategies for domain typesprop-custom-strategies
- Use shrinking to find minimal failing casesprop-shrinking
- Test invariants instead of specific valuesprop-invariant-testing
5. Test Fixtures and Setup (MEDIUM)
- Use rstest fixtures for test setupfix-rstest-fixtures
- Use rstest case for parameterized testsfix-rstest-parametrized
- Use TempDir for file system testsfix-temp-directories
- Use test-context for setup and teardownfix-test-context
- Use OnceCell for expensive shared setupfix-once-cell-shared-state
6. Assertions and Error Testing (MEDIUM)
- Assert specific error types not just is_errassert-specific-errors
- Use should_panic for panic testingassert-should-panic
- Implement Debug for clear failure messagesassert-debug-display
- Add context to assertions with custom messagesassert-custom-messages
- Use approximate comparison for floating pointassert-floating-point
- Assert collection contents not just lengthassert-collection-contents
7. CI Integration (MEDIUM)
- Use cargo-nextest for faster CIci-cargo-nextest
- Cache Cargo dependencies in CIci-caching
- Ensure test isolation in parallel CIci-test-isolation
- Generate coverage reports in CIci-coverage
8. Test Performance (LOW-MEDIUM)
- Reduce test compilation timeperf-compile-time
- Filter tests for faster feedback loopsperf-test-filtering
- Avoid real IO in unit testsperf-avoid-io-in-unit-tests
- Configure parallel test threadsperf-parallel-test-execution
- Benchmark critical paths with Criterionperf-benchmark-critical-paths
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 |