Understanding Test Driven Development with Python

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 prime.py 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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s