AgentSkillsCN

tdd-discipline

适用于实现功能、修复 bug 或编写任何生产代码——强制执行 RED-GREEN-REFACTOR 循环,即测试必须先失败,然后才能编写实现代码。在开始编码前激活。

SKILL.md
--- frontmatter
name: tdd-discipline
description: Use when implementing features, fixing bugs, or writing any production code - enforces RED-GREEN-REFACTOR cycle where tests must fail before writing implementation code. Activates before coding begins.

Test-Driven Development

Write the test first. Watch it fail. Write minimal code to pass.

Core principle: If you didn't watch the test fail, you don't know if it tests the right thing.

The Iron Law

code
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST

Write code before the test? Delete it. Start over.

No exceptions:

  • Don't keep it as "reference"
  • Don't "adapt" it while writing tests
  • Delete means delete

Red-Green-Refactor

RED - Write Failing Test

Write one minimal test showing what should happen. Run it. Watch it fail.

Verify failure is correct:

  • Test fails (not errors)
  • Fails because feature missing (not typos)
  • Failure message matches expectation

GREEN - Minimal Code

Write simplest code to pass the test. Nothing more.

Don't:

  • Add features beyond the test
  • Refactor other code
  • "Improve" beyond what test requires

REFACTOR - Clean Up

After green only:

  • Remove duplication
  • Improve names
  • Extract helpers

Keep tests green. Don't add behavior.

Common Rationalizations

ExcuseReality
"Too simple to test"Simple code breaks. Test takes 30 seconds.
"I'll test after"Tests passing immediately prove nothing.
"Already manually tested"Ad-hoc ≠ systematic. No record, can't re-run.
"Deleting X hours is wasteful"Sunk cost fallacy. Keeping unverified code is debt.
"Need to explore first"Fine. Throw away exploration, start with TDD.
"TDD will slow me down"TDD faster than debugging.

Red Flags - STOP and Start Over

  • Code before test
  • Test passes immediately (didn't see it fail)
  • Can't explain why test failed
  • "Just this once"
  • "This is different because..."

All of these mean: Delete code. Start over with TDD.

Quick Reference

PhaseActionVerify
REDWrite testFails for right reason
GREENMinimal codeTest passes, others still pass
REFACTORClean upAll tests still green

When Stuck

ProblemSolution
Don't know how to testWrite wished-for API first
Test too complicatedDesign too complicated. Simplify.
Must mock everythingCode too coupled. Refactor.