AgentSkillsCN

tdd-workflow

测试驱动开发工作流。RED-GREEN-REFACTOR循环。在实现新功能或修复缺陷时使用。

SKILL.md
--- frontmatter
name: tdd-workflow
description: Test-Driven Development workflow. RED-GREEN-REFACTOR cycle. Use when implementing new features or fixing bugs.
allowed-tools: Read, Write, Edit, Glob, Grep, Bash

TDD Workflow

Write tests first, code second.

The TDD Cycle

code
🔴 RED     → Write failing test
    ↓
🟢 GREEN   → Write minimal code to pass
    ↓
🔵 REFACTOR → Improve code quality
    ↓
   Repeat...

The Three Laws

  1. Write production code only to make a failing test pass
  2. Write only enough test to demonstrate failure
  3. Write only enough code to make the test pass

RED Phase

What to Test

FocusExample
Behavior"should add two numbers"
Edge cases"should handle empty input"
Error states"should throw for invalid data"

Rules

  • Test MUST fail first
  • Test name describes expected behavior
  • One assertion per test (ideally)

GREEN Phase

Minimum Code

PrincipleMeaning
YAGNIYou Aren't Gonna Need It
Simplest thingWrite the minimum to pass
No optimizationJust make it work

Rules

  • Don't write unneeded code
  • Don't optimize yet
  • Pass the test, nothing more

REFACTOR Phase

What to Improve

AreaAction
DuplicationExtract common code
NamingMake intent clear
StructureImprove organization
ComplexitySimplify logic

Rules

  • All tests must stay green
  • Small incremental changes
  • Commit after each refactor

AAA Pattern

Every test follows:

code
ARRANGE → Set up test data
ACT     → Execute code under test
ASSERT  → Verify expected outcome

When to Use TDD

ScenarioTDD Value
New featureHigh
Bug fixHigh (test first)
Complex logicHigh
ExploratoryLow (spike first)
UI layoutLow

Test Prioritization

PriorityTest Type
1Happy path
2Error cases
3Edge cases
4Performance

Anti-Patterns

❌ Don't✅ Do
Skip RED phaseWatch test fail first
Write tests afterWrite tests before
Over-engineer initialKeep it simple
Multiple assertsOne behavior per test
Test implementationTest behavior

Unity Testing

Edit Mode Tests

  • Fast, no scene required
  • Pure logic testing
  • Use for: calculations, data validation

Play Mode Tests

  • Requires scene
  • Tests MonoBehaviour lifecycle
  • Use for: integration, game flow
csharp
[Test]
public void Damage_ReducesHealth()
{
    // Arrange
    var unit = new Unit { Health = 100 };

    // Act
    unit.TakeDamage(30);

    // Assert
    Assert.AreEqual(70, unit.Health);
}

The test is the specification. If you can't write a test, you don't understand the requirement.