重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
npx skills add https://github.com/rtk-ai/rtk --skill rtk-tdd循环:红(测试失败) -> 绿(最小化通过) -> 重构(清理,运行 cargo test)
1. 在**同一个文件**的 `#[cfg(test)] mod tests` 中编写测试
2. 运行 `cargo test MODULE::tests::test_name` —— 必须**失败**(红)
3. 在函数中实现最小化代码
4. 运行 `cargo test MODULE::tests::test_name` —— 必须**通过**(绿)
5. 如有需要则重构,并重新运行 cargo test(保持绿色)
6. 运行 `cargo fmt && cargo clippy --all-targets && cargo test` (最终检查)
切勿跳过第 2 步。如果测试立即通过,则它没有测试任何内容。
| 模式 | 用途 | 适用场景 |
|---|---|---|
| 准备-执行-断言 | 每个测试的基本结构 | 始终 |
assert_eq! / |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
assert!| 直接比较 / 布尔值 |
| 确定性值 |
assert!(result.is_err()) | 错误路径测试 | 无效输入 |
Result<()> 返回类型 | 使用 ? 运算符的测试 | 可能失败的函数 |
#[should_panic] | 预期的恐慌 | 不变量、前置条件 |
tempfile::NamedTempFile | 文件/I/O 测试 | 依赖文件系统的代码 |
| 代码类型 | 测试模式 | 示例 |
|---|---|---|
| 纯函数(str -> str) | 输入字面量 -> 断言输出 | assert_eq!(truncate("hello", 3), "...") |
| 解析/过滤 | 原始字符串 -> 过滤 -> 包含/不包含 | assert!(filter(raw).contains("expected")) |
| 验证/安全 | 边界输入 -> 断言布尔值 | assert!(!is_valid("../etc/passwd")) |
| 错误处理 | 错误输入 -> is_err() | assert!(parse("garbage").is_err()) |
| 结构体/枚举往返 | 构造 -> 序列化 -> 反序列化 -> 相等 | assert_eq!(from_str(to_str(x)), x) |
test_{函数}_{场景}
test_{函数}_{输入类型}
示例:test_truncate_edge_case, test_parse_invalid_input, test_filter_empty_string
Command::new() 的函数 -> 测试解析器,而非执行过程std::process::exit() -> 先重构为返回 Result,然后测试该 Resultcargo fmt --all --check
cargo clippy --all-targets
cargo test
以上 3 项必须全部通过。没有例外。没有充分文档说明的理由,不得使用 #[allow(...)]。
每周安装次数
51
代码仓库
GitHub 星标数
14.3K
首次出现
2026 年 2 月 17 日
安全审计
安装于
codex49
kimi-cli48
gemini-cli48
amp48
cline48
github-copilot48
Cycle: RED (test fails) -> GREEN (minimum to pass) -> REFACTOR (cleanup, cargo test)
1. Write test in #[cfg(test)] mod tests of the SAME file
2. cargo test MODULE::tests::test_name -- must FAIL (red)
3. Implement the minimum in the function
4. cargo test MODULE::tests::test_name -- must PASS (green)
5. Refactor if needed, re-run cargo test (still green)
6. cargo fmt && cargo clippy --all-targets && cargo test (final gate)
Never skip step 2. If the test passes immediately, it tests nothing.
| Pattern | Usage | When |
|---|---|---|
| Arrange-Act-Assert | Base structure for every test | Always |
assert_eq! / assert! | Direct comparison / booleans | Deterministic values |
assert!(result.is_err()) | Error path testing | Invalid inputs |
Result<()> return type | Tests with ? operator | Fallible functions |
#[should_panic] | Expected panic | Invariants, preconditions |
tempfile::NamedTempFile | File/I/O tests | Filesystem-dependent code |
| Code Type | Test Pattern | Example |
|---|---|---|
| Pure function (str -> str) | Input literal -> assert output | assert_eq!(truncate("hello", 3), "...") |
| Parsing/filtering | Raw string -> filter -> contains/not-contains | assert!(filter(raw).contains("expected")) |
| Validation/security | Boundary inputs -> assert bool | assert!(!is_valid("../etc/passwd")) |
| Error handling | Bad input -> is_err() | assert!(parse("garbage").is_err()) |
test_{function}_{scenario}
test_{function}_{input_type}
Examples: test_truncate_edge_case, test_parse_invalid_input, test_filter_empty_string
Command::new() -> test the parser, not the executionstd::process::exit() -> refactor to Result first, then test the Resultcargo fmt --all --check
cargo clippy --all-targets
cargo test
All 3 must pass. No exceptions. No #[allow(...)] without documented justification.
Weekly Installs
51
Repository
GitHub Stars
14.3K
First Seen
Feb 17, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
codex49
kimi-cli48
gemini-cli48
amp48
cline48
github-copilot48
测试策略完整指南:单元/集成/E2E测试金字塔与自动化实践
11,200 周安装
| Struct/enum roundtrip | Construct -> serialize -> deserialize -> eq | assert_eq!(from_str(to_str(x)), x) |