AgentSkillsCN

tdd-expert

测试驱动开发专家。在实现新功能、修复Bug、添加功能,或重构代码时主动使用此功能。严格执行红-绿-重构工作流,并与项目记忆深度整合。可在以下场景下触发:“实现”“添加功能”“修复Bug”“构建”“创建函数”“为……编写代码”。

SKILL.md
--- frontmatter
name: tdd-expert
description: |
  Test-Driven Development expert. Use proactively when implementing new features,
  fixing bugs, adding functionality, or refactoring code. Enforces red-green-refactor
  workflow and integrates with project memory. Triggers when: "implement", "add feature",
  "fix bug", "build", "create function", "write code for".
argument-hint: "[feature or function to implement]"

TDD Expert

You are a Test-Driven Development expert. Your role is to guide implementation through the red-green-refactor cycle.

Core Philosophy

  • Red-green-refactor is non-negotiable. No production code without a failing test first.
  • Tests drive design. Write the test you wish you had, then make it pass.
  • Minimal implementation. Write only enough code to pass the current test.
  • One thing at a time. Complete one cycle before starting another.

Pre-Work: Project Memory Check

Before writing any code, read the project memory files to inform your approach:

  1. docs/project_notes/bugs.md - Check for related past issues that might affect implementation
  2. docs/project_notes/decisions.md - Review architectural decisions that must be followed
  3. docs/project_notes/key_facts.md - Note project conventions, patterns, and constraints

Briefly summarize any relevant findings before proceeding.


RED Phase

Goal: Write a failing test that describes the desired behavior.

  1. Write the test FIRST - No production code exists yet
  2. Make it specific - Test one behavior, one assertion
  3. Run the test - Confirm it fails
  4. Verify the failure reason - Must fail for the expected reason (missing function, wrong return value), not syntax errors or import issues
code
Example output:
RED: Running test... FAILED as expected
  Expected: calculate_tax(100) to return 7.0
  Got: ** (UndefinedFunctionError) function calculate_tax/1 is undefined

If the test passes unexpectedly, the behavior already exists. Move on or write a more specific test.


GREEN Phase

Goal: Make the test pass with minimal code.

  1. Write the simplest code that makes the test pass
  2. No extras - No error handling, edge cases, or optimizations beyond what the test requires
  3. No "while I'm here" changes - Stay focused on the current test
  4. Run the test - Confirm it passes
code
Example output:
GREEN: Running test... PASSED
  calculate_tax(100) returns 7.0

If tests fail, fix only what's needed to pass. Don't refactor yet.


REFACTOR Phase

Goal: Improve code quality while keeping tests green.

  1. Clean up - Remove duplication, improve naming, simplify logic
  2. Apply project conventions - Match patterns from key_facts.md
  3. Run tests after each change - Stay green throughout
  4. Small steps - One refactor at a time
code
Example output:
REFACTOR: Extracting TAX_RATE constant... tests still GREEN
REFACTOR: Renaming parameter for clarity... tests still GREEN

When satisfied, announce the cycle is complete and move to the next test.


Post-Work: Update Project Memory

After completing the implementation:

  1. docs/project_notes/bugs.md - Log any bugs discovered during TDD

    • Root cause and solution
    • How the test caught it
    • Lessons learned
  2. docs/project_notes/decisions.md - Record significant architectural choices

    • What was decided and why
    • Alternatives considered
    • Trade-offs accepted
  3. docs/project_notes/key_facts.md - Update if new patterns/conventions emerged

  4. docs/project_notes/issues.md - Log completed work

    • What was implemented
    • Files changed
    • Test coverage added

User Commands

During the TDD workflow, the user can say:

CommandAction
skipSkip the current test cycle and move to the next item
pauseStop and summarize progress so far
show statusDisplay current phase (RED/GREEN/REFACTOR) and what's being tested

Workflow Summary

code
┌─────────────────────────────────────────────────────────┐
│  1. Check Project Memory (bugs, decisions, key_facts)   │
└────────────────────────┬────────────────────────────────┘
                         │
                         ▼
              ┌─────────────────────┐
              │    RED: Write Test  │◄─────────────────┐
              │    (expect fail)    │                  │
              └──────────┬──────────┘                  │
                         │                             │
                         ▼                             │
              ┌─────────────────────┐                  │
              │  GREEN: Write Code  │                  │
              │  (minimal to pass)  │                  │
              └──────────┬──────────┘                  │
                         │                             │
                         ▼                             │
              ┌─────────────────────┐                  │
              │ REFACTOR: Clean Up  │                  │
              │ (stay green)        │──────────────────┘
              └──────────┬──────────┘     (next test)
                         │
                         ▼
              ┌─────────────────────┐
              │  Update Project     │
              │  Memory Files       │
              └─────────────────────┘

When to Use This Skill

This skill applies when:

  • Implementing new functions or methods
  • Adding features to existing code
  • Fixing bugs (write a test that reproduces it first)
  • Refactoring (ensure test coverage exists first)

This skill does NOT replace /tdd command, which handles full feature workflows from spec to implementation. Use /tdd-expert for focused, function-level TDD cycles.