用途
该技能面向 Rust 项目的测试与质量保障工作,适用于编写、组织和运行 Rust 测试的场景,包括单元测试、集成测试、文档测试(doc-tests)、proptest 属性测试、criterion 基准测试以及 cargo-mutants 变异测试。它不负责 CI 流水线接线与门禁排序(这部分属于 rust-tooling-cicd),也不覆盖非 Rust 测试套件。
主要能力
技能按测试类型给出位置与运行方式对照:
- 单元测试:位于源文件内的
#[cfg(test)] mod tests,与代码同 crate,可访问私有项。 - 集成测试:位于
tests/*.rs,每个文件是独立 crate,仅能访问公开 API。 - 文档测试:位于
///文档中的 ```rust 代码块,会被编译并作为示例运行。 - 属性测试:使用 proptest,可放在单元或集成测试中,生成随机输入并支持收缩(shrinking)。
- 基准测试:使用 criterion,位于
benches/*.rs,做统计计时而非正确性验证。
技能还提供参考文档与模板:test-organization.md(决定单元/集成/文档测试及用 nextest 运行)、property-and-mutation.md(添加 proptest 策略或 cargo-mutants)、test-suite.md(搭建单元+集成+文档+proptest 脚手架)、criterion-bench.md(添加 criterion 基准)。
使用方式
技能规定了一套强制性的智能体工作流:在任何测试工作之前,并行启动 3 个智能体,每个 Agent 调用各带一个名称:
- fuse-ai-pilot:explore-codebase —— 梳理现有 tests/、#[cfg(test)]、benches/。
- fuse-ai-pilot:research-expert —— 通过 Context7/Exa 核实当前 nextest/proptest/criterion 文档。
- mcp__context7__query-docs —— 检查 crate 特定 API(proptest 策略、criterion 分组)。
实现之后,运行 fuse-ai-pilot:sniper 进行验证。
快速参考命令(正确配对):
cargo nextest run --all-features # 单元 + 集成,快速、并行
cargo test --doc # 文档测试 —— nextest 不覆盖
属性测试骨架:
use proptest::prelude::*;
proptest! {
#[test]
fn round_trips(n in 0u32..10_000) {
prop_assert_eq!(decode(&encode(n)), n);
}
}
关键规则
- nextest 不运行文档测试:
cargo nextest run按设计跳过它们,必须始终与cargo test --doc配对,绝不能把 nextest 通过当作完整覆盖。 - 集成测试只能看到公开 API:若测试需要私有项,应放在
#[cfg(test)]而非tests/。 - 提交
proptest-regressions/:持久化的失败种子必须纳入版本控制,以便失败可复现。 - criterion 基准需
harness = false:必须在 Cargo.toml 中设置,否则内置 bench harness 会冲突。 - 变异测试是门禁而非冒烟测试:
cargo mutants很慢,应在计划性 CI 中运行,而非每次推送。
目录结构
my_crate/
├── src/
│ └── lib.rs # 单元测试在 #[cfg(test)] + 文档测试在 ///
├── tests/
│ └── api.rs # 集成测试(公开 API)
├── benches/
│ └── throughput.rs # criterion, harness = false
└── proptest-regressions/ # 提交的失败种子
完整示例见 test-suite.md。
最佳实践
应当:在每个 CI 门禁中同时运行 cargo nextest run 与 cargo test --doc;让集成测试针对公开 API 保持黑盒;proptest 从最廉价的属性开始(“不 panic”);基准按所测操作命名而非函数名。
不应:假设 nextest 覆盖了文档测试(它从不覆盖);把计时基准放进 #[test](应使用 benches/ 中的 criterion);把 proptest-regressions/ 加入 gitignore(应提交);每次推送都运行 cargo mutants(应改为计划性运行)。
适用场景与限制
适用于 Rust 项目的测试编写、组织与运行,以及属性测试、基准测试和变异测试的引入。限制在于:不处理 CI 流水线接线与门禁排序,不覆盖非 Rust 测试套件;关于收费、价格、平台等信息暂未提供。