test-optimization
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTest Optimization
测试优化
Advanced test optimization with cargo-nextest, property testing, and benchmarking.
借助cargo-nextest、属性测试和基准测试实现高级测试优化。
cargo-nextest (Primary Test Runner)
cargo-nextest(主要测试运行器)
bash
cargo nextest run --all # all tests
cargo nextest run --profile ci # CI with retries + JUnit XML
cargo nextest run --profile nightly # extended timeouts
cargo nextest run -E 'package(memory-core)' # filterset DSL
cargo test --doc --all # doctests (nextest limitation)bash
cargo nextest run --all # 所有测试
cargo nextest run --profile ci # CI环境(含重试+JUnit XML输出)
cargo nextest run --profile nightly # 延长超时时间
cargo nextest run -E 'package(memory-core)' # 使用过滤集DSL
cargo test --doc --all # 文档测试(nextest的局限性)Configuration (.config/nextest.toml)
配置文件(.config/nextest.toml)
toml
[profile.default]
retries = 0
slow-timeout = { period = "60s", terminate-after = 2 }
fail-fast = false
[profile.ci]
retries = 2
slow-timeout = { period = "30s", terminate-after = 3 }
failure-output = "immediate-final"
junit.path = "target/nextest/ci/junit.xml"
[profile.nightly]
retries = 3
slow-timeout = { period = "120s", terminate-after = 2 }toml
[profile.default]
retries = 0
slow-timeout = { period = "60s", terminate-after = 2 }
fail-fast = false
[profile.ci]
retries = 2
slow-timeout = { period = "30s", terminate-after = 3 }
failure-output = "immediate-final"
junit.path = "target/nextest/ci/junit.xml"
[profile.nightly]
retries = 3
slow-timeout = { period = "120s", terminate-after = 2 }Mutation Testing (cargo-mutants)
变异测试(cargo-mutants)
Verifies tests actually catch bugs by injecting mutations:
bash
cargo mutants -p memory-core --timeout 120 --jobs 4 -- --lib- Acceptance: <20% missed mutants in core business logic
- Frequency: Nightly CI or pre-release
- Scope: Start with memory-core, expand incrementally
通过注入变异来验证测试是否真的能发现bug:
bash
cargo mutants -p memory-core --timeout 120 --jobs 4 -- --lib- 验收标准:核心业务逻辑中未被检测到的变异占比<20%
- 执行频率:夜间CI或预发布阶段
- 覆盖范围:从memory-core开始,逐步扩展
Property-Based Testing (proptest)
基于属性的测试(proptest)
rust
proptest! {
#[test]
fn serialization_roundtrip(episode in any_episode_strategy()) {
let bytes = postcard::to_allocvec(&episode).unwrap();
let decoded: Episode = postcard::from_bytes(&bytes).unwrap();
assert_eq!(episode, decoded);
}
}rust
proptest! {
#[test]
fn serialization_roundtrip(episode in any_episode_strategy()) {
let bytes = postcard::to_allocvec(&episode).unwrap();
let decoded: Episode = postcard::from_bytes(&bytes).unwrap();
assert_eq!(episode, decoded);
}
}Snapshot Testing (insta)
快照测试(insta)
rust
#[test]
fn test_mcp_response_format() {
let response = build_tool_response("search_patterns", ¶ms);
insta::assert_json_snapshot!(response);
}bash
cargo insta test # run snapshot tests
cargo insta review # accept/reject changesrust
#[test]
fn test_mcp_response_format() {
let response = build_tool_response("search_patterns", ¶ms);
insta::assert_json_snapshot!(response);
}bash
cargo insta test # 运行快照测试
cargo insta review # 接受/拒绝变更Performance Targets
性能指标
| Operation | Target | Actual |
|---|---|---|
| Episode Creation | < 50ms | ~2.5 µs |
| Step Logging | < 20ms | ~1.1 µs |
| Pattern Extraction | < 1000ms | ~10.4 µs |
| Memory Retrieval | < 100ms | ~721 µs |
| 操作 | 目标值 | 实际值 |
|---|---|---|
| Episode创建 | < 50ms | ~2.5 µs |
| 步骤日志记录 | < 20ms | ~1.1 µs |
| 模式提取 | < 1000ms | ~10.4 µs |
| 内存检索 | < 100ms | ~721 µs |
Best Practices
最佳实践
- Use nextest profiles for dev/CI/nightly separation
- Implement property tests for serialization roundtrips and state machines
- Use snapshot tests for MCP responses, CLI output
- Run mutation testing before releases
- Monitor test duration and coverage trends
- 使用nextest配置文件区分开发/CI/夜间环境
- 为序列化往返和状态机实现基于属性的测试
- 为MCP响应、CLI输出使用快照测试
- 发布前运行变异测试
- 监控测试时长和覆盖率趋势
References
参考资料
- ADR-033: Modern Testing Strategy
- TESTING.md
- ADR-033: 现代测试策略
- TESTING.md