Difficulty: Beginner
Estimated Time: 10 minutes

Test Driven Development

What is TDD

Test driven development refers to the act of writing the tests first and only writing application code when there is a failing test written first. It is also referred to as red, green refactor.

  • Red create a failing test. Keep the test as small as possible
  • Green write just enough code to make the test pass
  • Refactor rework the code without changing the functionality (keep the tests green).
  • Repeat

Why would you want to do TDD

  • Means that you get to the end of writing code with tests already written
  • Forces the consideration of how to test the code before it has been written
  • Can highlight unclear requirements earlier in the development process


This has been a brief introduction to test driven development. A simple, if slightly contrived, example has been used to introduce the concept of writing the tests first, fixing the test and the refactoring whilst keeping the tests passing.

Keep tests small

By keeping the tests small and incrementally adding functionality, the code is kept in a working state. This has the advantage being able to commit to master what has been completed if what you are working on changes.


TDD, like many other skills, is something that gets better with practice. A great way to extend your skills is to regularly be very strict with yourself and not write any code unless there is a failing test first.

Introduction to TDD

Step 1 of 9

Step 1

A Simple Example

The requirement:

  • Create a function that will take a variable amount of arguments and return the sum.

This example already contains a basic test framework. You can run it as soon as the environment is ready.

python3 ./tests.py