Test-Driven Development (aka. TDD)

Learn the basics of test-driven development, including how it works, its benefits, and when it shouldn’t be applied.

Test-driven development (TDD) is a software design technique based on tests. It’s based on an iterative style of writing software. This process involves three phases:

  1. Write a failing test
  2. Make the failing test pass
  3. Refactor the code written

This iterative approach has to be followed every time we would like to add new functionalities to our program.

Note: Seeing a test fail is as important as seeing it pass.

This means that to ensure our tests are meaningful, we need to see them fail first.

The mantra of TDD

Before diving into TDD, let’s see what its constraints are:

  • We are not allowed to write any production code unless it is to make a failing unit test pass.
  • We are not allowed to write more of a unit test than is sufficient to fail, and compilation failures are failures.
  • We are not allowed to write any more production code than is sufficient to pass the one failing unit test.

Red, green, refactor!

Let’s visualize the three stages of test-driven development and their relationships:

Get hands-on with 1200+ tech skills courses.