Skip to main content

Test Driven Development

Test-driven development (TDD) is a software development process, similar to BDD the encourages developers to build test cases that represent the exepcted outcomes before the development of the actual code.

What is TDD exactly?

The basic concept of TDD is that all code is written in order to solve a test case. More specifically:

  • You cannot write any code unless there is a failing test you want to pass
  • You cannot write any more of a unit test than is sufficient for it to fail
  • You cannot write any more production code than is sufficient to pass a failing test

Why use TDD?

TDD has several benefits:

  • Robust code enables faster innovation and continuous delivery
  • Low risk of braking code as the code is flexible and extensible so can be refactored or moved with minimal risk
  • TDD code is easy to test (thats one of the principles), meaning its easy to understand
  • Productivity: The requirements are implemented as efficiently as possible because only the function that is needed is written

Red, Green, Refactor

Another interesting concept in TDD is the Red, Green, Refactor idea or process.

Essentially, it states the following:

  1. Write a test that fails: Red
  2. Write code that passes test: Green
  3. Refactor code so its "cleaner" (but test still passes): Refactor

If you want to learn more about TDD consider the following resources: