How-toSelected

AGENTS.md Rules for Testing AI-Generated Code

Why we picked it

The core insight is that post-hoc unit tests written by agents validate nothing — they assert exactly what the code already does, creating false coverage. Engineers using Claude Code, Codex or similar agents can paste these three lines into their AGENTS.md to shift verification from green checkmarks to inspectable evidence. The failure-mode-first rule also turns tests into design documents rather than afterthoughts.

Ansh Nanda shared three testing rules he places at the top of his AGENTS.

Body

md file to stop AI agents from over-testing.Rule one bans writing unit tests after code is written, since post-hoc tests often just mirror the implementation.

Rule two prefers E2E tests as the sole verification mechanism and requires a verifiable, repeatable artifact such as an output file, screen recording or report.

Rule three demands listing all possible failure modes in isolation tests before writing any code.

Original · 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📊 2 ⚡

Industry Coordinates & Source Verification

This report is categorized under AITOP's "How-to" desk, focusing on US frontier AI labs and Silicon Valley technology leaders. Within our cross-regional analytical framework, it directly maps to AI developer toolchains, coding agents, and engineering workflows. Key entities and technical topics under continuous tracking include AGENTS.md, coding assistants, E2E testing, prompt engineering.

Source attribution: curated from shao__meng (@shao__meng).