T

TDD Cycle Command

Orchestrates test-driven development with a strict red-green-refactor cycle. Write the failing test first, implement the minimum code to pass, then refactor. Ensures high test coverage and clean design through disciplined TDD practice.

CommandCommunitytestingv1.0.0MIT
0 views0 copies

Command

/tdd

Description

Guides you through a strict test-driven development cycle: write a failing test (red), implement the minimum code to make it pass (green), then refactor while keeping tests green. Enforces discipline by verifying each phase before moving to the next.

Behavior

Arguments

  • $ARGUMENTS -- What to implement (e.g., "password validation function with strength scoring")

The TDD Cycle

Step 1: RED -- Write the Failing Test

  1. Create or open the test file
  2. Write a single, focused test case for the smallest unit of behavior
  3. Run the test and verify it fails with the expected reason
  4. The test should fail because the function doesn't exist or returns the wrong value -- NOT because of syntax errors
# Run the test -- must FAIL npm test -- --grep "should reject passwords shorter than 8 characters" # Expected: FAIL (function not implemented yet)

Step 2: GREEN -- Write Minimum Code to Pass

  1. Write the simplest implementation that makes the test pass
  2. Do NOT add extra functionality, handle edge cases, or optimize
  3. Hardcoded return values are acceptable if they make the test pass
  4. Run the test and verify it passes
# Run the test -- must PASS npm test -- --grep "should reject passwords shorter than 8 characters" # Expected: PASS

Step 3: REFACTOR -- Improve While Green

  1. Clean up the implementation (remove duplication, improve names)
  2. Run all tests after each refactor step
  3. If any test fails, undo the last change immediately
  4. Do NOT add new behavior during refactoring
# Run ALL tests -- must ALL pass npm test # Expected: ALL PASS

Step 4: Repeat

Add the next test case and restart the cycle:

RED:      "should accept passwords with 8+ characters"
GREEN:    Add length check >= 8
REFACTOR: Extract length constant

RED:      "should require at least one uppercase letter"
GREEN:    Add /[A-Z]/ check
REFACTOR: Combine regex patterns

RED:      "should return strength score 0-4"
GREEN:    Add scoring logic
REFACTOR: Extract scoring rules into config

Rules

  • NEVER write implementation before the test
  • NEVER write more than one failing test at a time
  • NEVER add behavior during the refactor phase
  • Each test should be independent and isolated
  • Test names should describe behavior, not implementation
  • Keep the cycle fast -- each iteration should take 2-5 minutes

Output Format

Report each cycle iteration:

## TDD Cycle: Password Validator ### Iteration 1 - RED: `should reject empty passwords` -- FAILS (function not found) - GREEN: Created `validatePassword()` returning `{valid: false}` for empty input -- PASSES - REFACTOR: No refactoring needed yet ### Iteration 2 - RED: `should reject passwords under 8 chars` -- FAILS (no length check) - GREEN: Added `input.length >= 8` check -- PASSES - REFACTOR: Extracted `MIN_LENGTH = 8` constant ### Current Test Coverage: 4 tests, 100% on validatePassword()

Examples

# TDD a new function /tdd password validation with strength scoring # TDD a React component /tdd search autocomplete component with debounce # TDD an API endpoint /tdd POST /api/bookmarks endpoint with validation
Community

Reviews

Write a review

No reviews yet. Be the first to review this template!

Similar Templates