该技能用于统一测试驱动开发(TDD)工作流,强制测试先行,并复用仓库既有测试模式。核心循环为 RED(先写失败测试并确认失败原因正确)→ GREEN(仅写让测试通过的最小实现)→ REFACTOR(在测试保持绿色的前提下清理实现与测试),每次变更后重跑测试。
三种输入模式:模式A从 .spec.md 规格进入,解析 Given/When/Then 验收条件,为每条条件生成带 todo!() 的测试桩,先用 cargo test --no-run -p <crate> 确认可编译但失败;可用 ralph_core::preflight::{extract_acceptance_criteria, extract_criteria_from_file, extract_all_criteria} 解析条件。模式B从 .code-task.md 任务进入,读取任务需求并覆盖正常、边界与错误场景。模式C从描述进入,适用于无规格或任务文件的临时任务。
写测试前先发现既有约定,例如 rg --files -g "crates//tests/.rs" 与 rg -n "#[cfg(test)]" crates/,并参考目标代码附近2-3个测试文件,沿用其模块布局、命名、断言风格及 fixture 工具。
proptest 仅在函数纯、输出确定且输入空间或边界非平凡时使用;无充分理由不新增该依赖。测试位置:规格对应单模块用内联 #[cfg(test)],跨模块用 crates/<crate>/tests/ 集成测试,CLI 行为放 crates/ralph-cli/tests/。完成事件需包含覆盖证据,如 ralph emit "build.done" "tests: pass, lint: pass, typecheck: pass, audit: pass, coverage: pass (82%)",可行时运行 cargo tarpaulin --out Html --output-dir coverage --skip-clean。反模式包括先写实现、生成无需实现即通过的测试、照搬其他 crate 测试、滥用 proptest、无覆盖证据就发完成事件。