TDD (Test-Driven Development): “Test First, Code Later”
TDD is a strict programming practice where developers write an automated test before they write a single line of functional code.
It follows a micro-cycle called “Red-Green-Refactor”:
-
Red: Write a test for a small new feature. Since the feature doesn’t exist yet, the test fails (turns red in the testing dashboard).
-
Green: Write the absolute minimum amount of code needed to make that test pass (turns green).
-
Refactor: Clean up and improve the code you just wrote, without changing its behavior, while keeping the test green.
Why do developers use TDD?
-
Fewer bugs: Because you are constantly testing tiny pieces of code, bugs are caught instantly, not months later in production.
-
Better design: Writing the test first forces the developer to think about how the code will be used before they write it, often leading to cleaner, more modular designs.
-
Safety net: You build up a huge suite of automated tests. If a developer accidentally breaks something later, the tests immediately tell them exactly where.