Definition
Test-Driven Development (TDD)
A software development process where you write the test before you write the code. It relies on a very short development cycle:
- Add a test for a small piece of functionality.
- Run the test and see it fail (Red).
- Write just enough code to make the test pass (Green).
- Refactor the code to improve design while keeping tests green.
Pros
- Design Feedback: Writing the test first forces you to design the API from the client’s perspective (usability).
- Rapid Feedback: Bugs are caught instantly.
- Testability: Code is testable by definition (since it was written to satisfy a test).
- Confidence: Enables aggressive refactoring later.
- Velocity: Developers can choose their speed by taking smaller or larger steps.
Cycle
- Red: Write a failing test (compilation error or assertion failure).
- Green: Make it pass as simply as possible (even if it means hardcoding).
- Refactor: Clean up duplication and improve structure.