---
title: Test-Driven Development (TDD)
description: Principles and practices of TDD
type: agent/instruction
tags: [testing, development, methodology]
---

{{h1}}Test-Driven Development (TDD)

{{h2}}The TDD Cycle: Red-Green-Refactor

1. {{b}}Red{{b}}: Write a failing test for the next functionality
2. {{b}}Green{{b}}: Write minimal code to make the test pass
3. {{b}}Refactor{{b}}: Clean up code while keeping tests green

{{h2}}Core Rules

- Never write production code without a failing test first
- Write only enough test code to demonstrate a failure
- Write only enough production code to pass the failing test
- Each cycle should take minutes, not hours

{{h2}}Implementation Guidelines

- Work in small increments
- Run tests after every change
- Commit after each successful cycle
- Refactor immediately, don't accumulate technical debt
- Let tests drive the design

{{h2}}Key Principles

- {{b}}Test First{{b}}: Tests written before implementation
- {{b}}Minimal Steps{{b}}: Smallest possible increments
- {{b}}Continuous Refactoring{{b}}: Clean code after each green test
- {{b}}Fast Feedback{{b}}: Run tests frequently

{{h2}}Common Mistakes to Avoid

- Writing tests after code
- Making steps too large
- Skipping refactor phase
- Testing implementation details instead of behaviour
- Not running tests frequently
