技巧精选

AI Agent 测试怎么写不跑偏?把这三条规则写进 AGENTS.md

精选理由

一位开发者把三条 Agent 测试规则贴在 AGENTS.md 顶部,核心思路是用 E2E 加可复现产物替代事后补的单测,写 Agent 工作流的可以抄走。

Ansh Nanda 分享了写在 AGENTS.md 顶部的三条 Agent 测试规则。第一条是严禁在写完代码后再补单元测试,因为事后补的单测往往只是复述实现逻辑,验证不了正确性。第二条是首选 E2E 测试作为唯一验证机制,且结束时必须产出可重跑、可检查的产物,如输出文件或录屏。第三条是若需隔离测试,先穷举所有可能的失败方式,再动手写代码,让测试承载设计意图。

原文 · shao__meng

AI Agent 要怎么写测试,还不会过度测试、为测试而测试 ? @anshnanda 分享了自己写在 AGENTS.md 顶部的三条规则,值得一看! 1. 严禁在写完代码之后再补单元测试。 事后补写的单测(尤其是 Agent 生成的)往往只是把实现逻辑复述一遍:代码怎么写的,测试就怎么断言。它验证不了正确性,只制造“有测试覆盖”的假象。dex 举例的那种“检查常量里包含某子串”正是这类无意义的同义反复。 2. 首选 E2E 测试作为唯一的验证机制,且结束时必须产出可验证、可复现的产物。 他要的是从真实用户路径出发验证功能确实能用,而判据不是“测试绿灯”,而是一份能留档、能重跑、能被人检查的证据(如输出文件、录屏、报告)。这把验证从“测试通过”升级为“拿出证据”。 3. 如果确有必要隔离测试某个系统,先穷举它所有可能的失败方式,再动手写代码。 这是失败模式驱动的前置测试:测试的价值在于预先写下“哪些情况会坏”的假设,让测试承载设计意图,而不是事后镜像实现。 AGENTS.md 中要加入顶部的原文: - NEVER write unit tests after you write code. - Highly prefer E2E tests as the sole testing mechanism. Use them to verify complex features work. At the end of E2E tests, produce a verifiable and repeatable artifact. - If you must test a system in isolation, FIRST write all the ways it could fail, THEN write the code. Ansh Nanda @anshnanda At the top of my AGENTS.md: - NEVER write unit tests after you write code. - Highly prefer E2E tests as the sole testing mechanism. Use them to verify complex features work. At the end of E2E tests, produce a verifiable and repeatable artifact. - If you must test a system in isolation, FIRST write all the ways it could fail, THEN write the code. 🔗 View Quoted Tweet 💬 1 🔄 0 ❤️ 2 👀 515 📊 2 ⚡