Author: Chetan Giridhar and Vishal Kanaujia

Published at: agilerecord, February 2012 Edition


Test Driven Development is still a nascent concept for many. Developers often have erroneous assumptions, preconceived notions about the concept, and may fail to understand its potential. This article explains TDD with an intuitive Python example.

Let’s start with what TDD is?

 TDD stands for Test Driven Development. According to Wikipedia, “Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines
a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.”

A typical TDD cycle comprises:

  • Writing a test
  • Run to fail
  • Develop the functionality (complete or partial in chunks)
  • Make the test pass
  • Refactor code (clean up)
  • Repeat the iteration

Ok! Ok! Enough of theory! Let’s get to the code!

Example: Developing code for validation of prime numbers with TDD

As a first step, let’s write a test. As there is no functionality developed, the test would fail. In the example below, the module contains a class and has a method isPrime() that aims to validate prime numbers but is not yet implemented.

You can download the complete magazine and this article from agilerecord