AgentSkillsCN

tdd

在任何代码实现任务中,此方案都应被严格执行。它倡导“红—绿—重构”循环开发模式:“如果你没有亲眼见证代码失败,你就无法确定它是否真正测试了正确的功能。”

SKILL.md
--- frontmatter
name: tdd
description: Use for any code implementation task. Enforces RED-GREEN-REFACTOR cycle. "If you didn't watch it fail, you don't know if it tests the right thing."
version: 1.0.0

Test-Driven Development Skill

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

The Cycle

code
    ┌──────────────────────────────────────────┐
    │                                          │
    ▼                                          │
┌───────┐    ┌───────┐    ┌──────────┐        │
│  RED  │ ──▶│ GREEN │ ──▶│ REFACTOR │ ───────┘
└───────┘    └───────┘    └──────────┘
Write test   Make pass    Clean up
Watch FAIL   Minimal code  Keep green

RED Phase

What to Do

  1. Write a single failing test
  2. Run the test
  3. Watch it fail
  4. Verify failure is for expected reason

Why "Watch It Fail" Matters

Without Watching FailWith Watching Fail
Test might always pass (bug in test)Confirms test can fail
Test might fail for wrong reasonConfirms test checks right thing
No proof test validates behaviorEvidence of validation

Red Phase Checklist

  • Test written for ONE behavior
  • Test run
  • Test failed
  • Failure is for expected reason (not typo, import error, etc.)

GREEN Phase

What to Do

  1. Write the minimum code to pass the test
  2. Run the test
  3. Watch it pass
  4. Nothing more - no extras, no "while I'm here"

What Minimum Means

Too MuchMinimum
Add error handling for edge casesJust make this test pass
Refactor related codeJust make this test pass
Add features "we'll need"Just make this test pass

Green Phase Checklist

  • Only code needed for this test written
  • Test run
  • Test passed
  • No other tests broken

REFACTOR Phase

What to Do

  1. Clean up the code
  2. Run ALL tests after each change
  3. Keep everything green
  4. Stop when code is clean

Safe Refactorings

  • Rename for clarity
  • Extract methods/functions
  • Remove duplication
  • Improve structure

Refactor Phase Checklist

  • Code is clean
  • All tests still pass
  • No new behavior added

Non-Negotiable Rules

[!CRITICAL] These are not guidelines. They are rules.

Rule 1: Test First

code
Production code written BEFORE failing test → DELETE and restart

No exceptions. No "I'll write the test after." That's not TDD.

Rule 2: Watch It Fail

code
Test not seen failing → Test not trusted

Running the test and seeing green immediately means something is wrong.

Rule 3: Minimal Green

code
Code beyond minimum → Remove it

YAGNI (You Aren't Gonna Need It). Add it when a test requires it.

Rule 4: Keep Tests Green

code
Refactoring breaks tests → Undo and retry smaller

Refactoring should never change behavior.


Integration with Verification

TDD is a specialized form of the verification skill:

Verification StepTDD Equivalent
IDENTIFYWrite the test
RUNExecute the test
READObserve failure/pass
VERIFYExpected behavior?
CLAIMOnly after green

Integration with Writing-Plans

Plans should include TDD structure:

markdown
## Task N: [Name]

1. Write failing test:
   ```typescript
   [complete test code]
  1. Run test:

    bash
    [command]
    

    Expected: FAIL with "[message]"

  2. Implement:

    typescript
    [complete implementation]
    
  3. Run test: Expected: PASS

code

---

## Deviation Logging

Code without test logged:

```yaml
- id: [auto]
  timestamp: [now]
  expected: "Failing test written before implementation"
  actual: "Implementation written without test"
  root_cause: untested_code
  fix: |
    Enforce TDD skill for all code tasks.
    Add test-first check to orchestration.

Common TDD Mistakes

MistakeProblemFix
Writing test after codeCan't verify test catches bugsDelete code, write test first
Not watching test failDon't know if test worksRun test before implementation
Making multiple tests pass at onceCan't isolate failuresOne test at a time
Refactoring while redChanges behaviorGet green first
Testing implementation detailsBrittle testsTest behavior, not code