/pstack:tdd
按需点名一句话先写一个会失败的测试,再修,再跑一遍确认它通过。
- 什么时候用
- 修 bug,而且在本地跑测试很方便。
/pstack:tdd implement用它和不用它
同一件事,直接跟 AI 说,和点名用 /pstack:tdd,AI 的做法不一样:
用 /pstack:tdd文档写了什么
原文分 4 节,约 450 个英文单词,中文译文约 850 字,读完约 2 分钟。点一节直接跳到全文。
开头说明有便宜的测试路径时,先写一个修前失败、修后通过的测试,太难测就换最接近的检查方式。
写法亮点
挑了几句原文:每句让 AI 做什么、为什么这样写管用、你写提示词时怎么借来用。
连失败也要验对原因
Confirm it fails for the intended reason.
确认它失败的原因正是你想要的那个。
- 让 AI
- 新测试跑红后,它还会看红的理由是不是这个 bug,而不是拼写错误或少装了依赖。
- 为什么管用
- 只说「先让测试失败」,AI 很容易拿一个随便什么原因的红灯交差;点明「为对的原因失败」,这一步才真能证明问题在哪。
- 借来用
- 让 AI 复现问题时加一句:「复现出来后,说说它报错的原因和我描述的是不是同一个。」
允许它说「这次不做」
Prefer no new test over a bad test.
宁可不新加测试,也不要加一个坏测试。
- 让 AI
- 测试不好写时,它会换一种验证方式并说明原因,而不是硬塞一个只测替身、没人敢动的测试。
- 为什么管用
- 流程写得越死,AI 越会为了走完流程凑出烂东西;明说「不做也行」,再定义什么算坏,它才敢停手。
- 借来用
- 布置有固定步骤的活时留个出口:「配图找不到合适的就空着,标一下,别拿不相关的图凑。」
说清以什么为准
The test should encode intended behavior, not mirror the current implementation.
测试应该写出应有的行为,而不是照抄现在的实现。
- 让 AI
- 它写的测试检查的是功能本该怎样,不是把现在有 bug 的代码原样抄一遍。
- 为什么管用
- AI 手边最近的参照就是现有代码;直接说出该对照「应有的行为」,它就不会把错的当标准。
- 借来用
- 让 AI 校对时说清依据:「以合同原文为准,不要以上一版译稿为准。」
规定汇报要带什么
Report the evidence, not just the outcome:
报告证据,而不只是结果:
- 让 AI
- 最后它会告诉你:修之前哪个测试红了、红成什么样,修之后哪次运行变绿了。
- 为什么管用
- 只要结果,AI 会说「修好了」;列出汇报必须带的几样证据,你不用追问就能自己判断。
- 借来用
- 让 AI 汇报时列清单:「说改了哪个文件、怎么测的、测试输出的最后几行。」
全文
AI 读到的就是这份 SKILL.md。可以看中文、看原文,或者一段一段对照着看。
descriptionUse only when the user explicitly asks for TDD, a failing test, or a regression test, OR when the bug has an obvious cheap local test target. Skip when the test path is unclear, expensive, integration-heavy, or not requested.
description只在这些时候用:用户明确要求 TDD、一个会失败的测试或一个回归测试;或者这个 bug 明显有一个便宜的本地测试可写。测试路径不清楚、代价高、很依赖集成环境,或者用户没要求时,跳过。
TDD Bug Fix
TDD Bug Fix(先写测试再修 bug)
When fixing a bug with a clear, cheap test path, make the broken behavior executable before changing production code. The goal is a focused regression test that fails before the fix and passes after it.
修 bug 时,如果有一条清楚又便宜的测试路径,就在改生产代码之前,先把出错的行为变成能跑的东西。目标是一个聚焦的回归测试(防止老问题再出现的测试):修之前失败,修之后通过。
Do not force a test when it would be impractical. If the available test would require broad harness setup, brittle mocks, slow end-to-end infrastructure, production-only state, vague reproduction steps, or large unrelated fixture churn, skip adding a new test and use the closest useful verification instead.
测试不现实时,不要硬写。如果能用的测试需要大范围搭建测试环境、很脆的模拟替身、很慢的端到端基础设施、只有生产环境才有的状态、说不清的复现步骤,或者大量改动不相干的测试数据,那就别新加测试,改用最接近、又有用的验证方式。
Workflow
流程
- Understand the bug. Identify the intended behavior, current behavior, affected path, and smallest observable reproduction.
- Choose the narrowest executable check. Prefer the closest unit, component, integration, or regression test already used for that codepath. If no practical test path is obvious, do not create one from scratch just to satisfy the workflow.
- Write the failing test first. Add the smallest focused test that would have caught the bug. The test should encode intended behavior, not mirror the current implementation.
- Run the new test before fixing. Confirm it fails for the intended reason. If it passes or fails for an unrelated reason, correct the test or reproduction before editing the implementation.
- Fix the bug. Make the smallest production change that satisfies the intended behavior while preserving nearby contracts.
- Rerun the regression test. Confirm the test now passes.
- 弄懂这个 bug。 找出应有的行为、现在的行为、受影响的代码路径,以及能看得见的最小复现。
- 挑最窄的、能跑的检查。 优先用这条代码路径已经在用的、最接近的单元、组件、集成或回归测试。如果看不出有什么现实的测试路径,不要只为了走完流程就从零造一个。
- 先写会失败的测试。 加一个最小的、聚焦的、本来能抓住这个 bug 的测试。测试应该写出应有的行为,而不是照抄现在的实现。
- 修之前先跑新测试。 确认它失败的原因正是你想要的那个。如果它通过了,或者因为不相干的原因失败,就先改好测试或复现,再去动实现。
- 修 bug。 做最小的生产代码改动,让行为符合应有的样子,同时保住附近代码的约定。
- 再跑一遍回归测试。 确认测试现在通过了。
If a Failing Test Is Impractical
如果写一个会失败的测试不现实
Use the closest executable regression check instead: a targeted script, manual reproduction command, browser automation, snapshot comparison, log assertion, or focused integration check.
就改用最接近的、能跑的回归检查:一个有针对性的脚本、一条手动复现的命令、浏览器自动化、截图对比、日志断言,或者一个聚焦的集成检查。
Prefer no new test over a bad test. A bad test is one that mostly tests mocks, encodes current implementation details, depends on timing or unrelated global state, needs expensive infrastructure for a small fix, or would be deleted immediately after proving the fix.
宁可不新加测试,也不要加一个坏测试。坏测试是指:大部分在测模拟替身,写死了现在的实现细节,依赖时间先后或不相干的全局状态,为一个小修复需要昂贵的基础设施,或者证明完修复后马上就会被删掉。
Guardrails
护栏
- Do not change tests merely to match a wrong implementation.
- Do not weaken existing assertions unless the expected behavior has genuinely changed and the reason is clear.
- Keep the regression test focused on the bug. Avoid broad fixture churn or unrelated coverage expansion.
- If the bug is flaky, make the test deterministic where possible and document the signal being locked down.
- If the bug exposes a broader class of failures, first land the focused regression path, then consider additional sibling coverage.
- 不要只为了迎合错误的实现去改测试。
- 不要放宽已有的断言,除非预期行为真的变了,而且原因很清楚。
- 回归测试只盯着这个 bug。避免大范围改动测试数据,或者顺手扩大不相干的覆盖范围。
- 如果这个 bug 时有时无,就尽量让测试结果稳定,并写明要固定住的是哪个信号。
- 如果这个 bug 暴露出一整类问题,先把聚焦的那条回归路径交上去,再考虑补上同类的其他覆盖。
Final Response
最后的回复
Report the evidence, not just the outcome:
报告证据,而不只是结果:
- Name the failing-before test or executable check and the failure it produced.
- Name the passing-after test run and any nearby validation performed.
- If failing-before evidence could not be demonstrated, state why and describe the closest regression check used instead.
- 点名修之前失败的测试或能跑的检查,以及它报出的失败。
- 点名修之后通过的那次测试运行,以及在附近做过的其他验证。
- 如果没能拿出修之前失败的证据,说明原因,并描述改用了哪个最接近的回归检查。
原文作者 Lauren Tan(poteto),MIT 许可。译文由本站翻译,核对到 pstack 镜像 97de40d。 在 GitHub 上看