rust-testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust Testing Best Practices
Rust测试最佳实践
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.
这是一份针对Rust应用的全面测试指南,涵盖CLI测试、库测试、异步模式以及CI集成。包含8个类别下的42条规则,按影响优先级排序,用于指导测试设计、模拟策略和CI优化。
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
在以下场景中参考本指南:
- 为Rust库或模块编写单元测试
- 为CLI应用创建集成测试
- 使用mockall或基于trait的设计设置模拟
- 使用Tokio测试异步代码
- 为Rust项目配置CI流水线
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 | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 测试组织 | 关键 | |
| 2 | 模拟与测试替身 | 关键 | |
| 3 | 异步测试 | 高 | |
| 4 | 属性化测试 | 高 | |
| 5 | 测试夹具与初始化 | 中 | |
| 6 | 断言与错误测试 | 中 | |
| 7 | CI集成 | 中 | |
| 8 | 测试性能 | 低-中 | |
Quick Reference
快速参考
1. Test Organization (CRITICAL)
1. 测试组织(关键)
- - Use cfg(test) modules for unit tests
org-unit-test-modules - - Place integration tests in tests directory
org-integration-tests-directory - - Use tests/common/mod.rs for shared utilities
org-shared-test-utilities - - Extract logic from main.rs into lib.rs
org-binary-crate-pattern - - Name tests after behavior not implementation
org-test-naming - - Use assert_cmd for CLI testing
org-test-cli-with-assert-cmd
- - 使用cfg(test)模块编写单元测试
org-unit-test-modules - - 将集成测试放在tests目录中
org-integration-tests-directory - - 使用tests/common/mod.rs存放共享工具
org-shared-test-utilities - - 将main.rs中的逻辑提取到lib.rs
org-binary-crate-pattern - - 按行为而非实现命名测试
org-test-naming - - 使用assert_cmd进行CLI测试
org-test-cli-with-assert-cmd
2. Mocking and Test Doubles (CRITICAL)
2. 模拟与测试替身(关键)
- - Design for testability with traits
mock-trait-based-design - - Use mockall automock for complex mocking
mock-automock-attribute - - Avoid mocking types you own
mock-avoid-mocking-owned-types - - Verify mock call counts explicitly
mock-expect-call-counts - - Use predicates to verify mock arguments
mock-predicate-arguments - - Use sequences for multiple return values
mock-returning-sequences - - Use mock! macro for static methods
mock-static-methods
- - 基于trait设计以提升可测试性
mock-trait-based-design - - 使用mockall的automock进行复杂模拟
mock-automock-attribute - - 避免模拟你自己拥有的类型
mock-avoid-mocking-owned-types - - 显式验证模拟的调用次数
mock-expect-call-counts - - 使用谓词验证模拟的参数
mock-predicate-arguments - - 使用序列处理多返回值
mock-returning-sequences - - 使用mock!宏模拟静态方法
mock-static-methods
3. Async Testing (HIGH)
3. 异步测试(高)
- - Use tokio::test for async test functions
async-tokio-test-macro - - Use paused time for timeout testing
async-time-control - - Use tokio_test for mocking async IO
async-mock-io - - Test spawn_blocking with multi-threaded runtime
async-spawn-blocking - - Use channels for testing async communication
async-test-channels
- - 使用tokio::test标记异步测试函数
async-tokio-test-macro - - 使用暂停时间进行超时测试
async-time-control - - 使用tokio_test模拟异步IO
async-mock-io - - 使用多线程运行时测试spawn_blocking
async-spawn-blocking - - 使用通道测试异步通信
async-test-channels
4. Property-Based Testing (HIGH)
4. 属性化测试(高)
- - Use proptest for property-based testing
prop-proptest-basics - - Create custom strategies for domain types
prop-custom-strategies - - Use shrinking to find minimal failing cases
prop-shrinking - - Test invariants instead of specific values
prop-invariant-testing
- - 使用proptest进行属性化测试
prop-proptest-basics - - 为领域类型创建自定义策略
prop-custom-strategies - - 使用收缩功能找到最小失败用例
prop-shrinking - - 测试不变量而非特定值
prop-invariant-testing
5. Test Fixtures and Setup (MEDIUM)
5. 测试夹具与初始化(中)
- - Use rstest fixtures for test setup
fix-rstest-fixtures - - Use rstest case for parameterized tests
fix-rstest-parametrized - - Use TempDir for file system tests
fix-temp-directories - - Use test-context for setup and teardown
fix-test-context - - Use OnceCell for expensive shared setup
fix-once-cell-shared-state
- - 使用rstest夹具进行测试初始化
fix-rstest-fixtures - - 使用rstest case进行参数化测试
fix-rstest-parametrized - - 使用TempDir进行文件系统测试
fix-temp-directories - - 使用test-context进行初始化和清理
fix-test-context - - 使用OnceCell处理昂贵的共享初始化
fix-once-cell-shared-state
6. Assertions and Error Testing (MEDIUM)
6. 断言与错误测试(中)
- - Assert specific error types not just is_err
assert-specific-errors - - Use should_panic for panic testing
assert-should-panic - - Implement Debug for clear failure messages
assert-debug-display - - Add context to assertions with custom messages
assert-custom-messages - - Use approximate comparison for floating point
assert-floating-point - - Assert collection contents not just length
assert-collection-contents
- - 断言特定错误类型而非仅检查is_err
assert-specific-errors - - 使用should_panic测试panic场景
assert-should-panic - - 实现Debug以获得清晰的失败信息
assert-debug-display - - 为断言添加自定义上下文信息
assert-custom-messages - - 对浮点数使用近似比较
assert-floating-point - - 断言集合内容而非仅检查长度
assert-collection-contents
7. CI Integration (MEDIUM)
7. CI集成(中)
- - Use cargo-nextest for faster CI
ci-cargo-nextest - - Cache Cargo dependencies in CI
ci-caching - - Ensure test isolation in parallel CI
ci-test-isolation - - Generate coverage reports in CI
ci-coverage
- - 使用cargo-nextest加速CI测试
ci-cargo-nextest - - 在CI中缓存Cargo依赖
ci-caching - - 确保并行CI中的测试隔离
ci-test-isolation - - 在CI中生成覆盖率报告
ci-coverage
8. Test Performance (LOW-MEDIUM)
8. 测试性能(低-中)
- - Reduce test compilation time
perf-compile-time - - Filter tests for faster feedback loops
perf-test-filtering - - Avoid real IO in unit tests
perf-avoid-io-in-unit-tests - - Configure parallel test threads
perf-parallel-test-execution - - Benchmark critical paths with Criterion
perf-benchmark-critical-paths
- - 减少测试编译时间
perf-compile-time - - 过滤测试以获得更快的反馈循环
perf-test-filtering - - 在单元测试中避免真实IO操作
perf-avoid-io-in-unit-tests - - 配置并行测试线程
perf-parallel-test-execution - - 使用Criterion对关键路径进行基准测试
perf-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 |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |