Skip to main content

Test Driven Development (TDD)

It's a methodology that focuses in creating the tests before developing the code. It originated from Agile and XP (Extreme Programming) practices.


The cycle Red-Green-Refactor:

  1. 🔴 Red: Create precise tests that define the desired behavior or functionality to be implemented. Since no code has been written yet, the test will fail.

  2. 🟢 Green: Write the simplest code necessary to make the failing test pass. The goal is to make the test pass and achieve the desired behavior. At this stage, code quality and efficiency are not the primary concerns.

  3. 🛠️ Refactor: Once the test is passing, you can safely refactor your code to improve its design, architecture, and performance.

  4. 🌀 Repeat: The Red-Green-Refactor cycle is repeated for each new feature or functionality to be implemented. This iterative process gradually builds up a comprehensive suite of tests and high-quality code.


Benefits of TDD

  • Improve code quality driving a cleaner and more maintainable code.
  • Reduce debugging time catching and fixing issues early
  • Increase confidence working as a documentation and testing the code
  • Makes refactoring safer since you're writing the test cases first.
  • Faster development in the long run.