用途:面向 Rust 项目的测试编写与运行,覆盖单元测试、集成测试、文档测试、异步测试、属性/快照/基准测试,以及 cargo test 与 cargo nextest 的用法。
主要能力:单元测试放在源码文件内,用 #[cfg(test)] mod tests 包裹并 use super::*;集成测试放在顶层 tests/ 目录,每个 .rs 文件是独立 crate,只能访问公开 API;文档测试写在 /// 注释中,仅对 lib crate 生效。支持 #[should_panic(expected = "...")]、#[ignore]、返回 Result<(), E> 的测试。异步测试用 #[tokio::test],需在 dev-dependencies 中引入 tokio 的 macros 与 rt 特性。
使用方式:cargo test 运行全部测试,可用 --lib、--tests、--test <name>、--doc 筛选;-- --ignored 只跑被忽略测试,-- --include-ignored 全部运行,-- --test-threads=1 单线程。cargo nextest run 提供进程隔离的更快运行器,但不运行文档测试,需另跑 cargo test --doc。属性测试用 proptest/quickcheck,快照用 insta,基准用 criterion 放在 benches/ 并设 harness = false。
限制:二进制 crate 的集成测试无法直接调用,需把逻辑移到 lib.rs;tests/common.rs 会被当作独立测试二进制,共享辅助代码应放 tests/common/mod.rs;#[should_panic] 不能与返回 Result 的测试同时使用;内置 #[bench] 仅 nightly 可用。其余细节暂未提供。