Archive for September, 2008

Test-Driven Development

September 5th, 2008
2
Digg me

Writing tests is pain because your code is not written to be testable. Why not try something called Test-Driven Developement?

Example: You need software which involves dogs… :) So you probably need a class called Dog. Instead of writing the class first, try to imagine what you would like the class to do. Don’t just imagine it; write a test which demonstrates how you want your dog to act.

class DogTest(unittest.TestCase):
    def testBark(self):
        dog = Dog()
        self.assertEqual('wuh', dog.bark())

You run the test and see it fail. Obviously, since there is no Dog yet! What you have done is that you’ve made a design decision, that you wan’t the dog object to bark by calling its bark method. Now you only need to implement it, and it’s a breeze since you already have the test to back it up:

class Dog():
    def bark(self):
        return 'wuh'

Congrats.. You’re test driving! Simple, isn’t it?

For more information try google or some of these links:
http://www.agiledata.org/essays/tdd.html
http://blog.objectmentor.com/articles/2007/07/17/testing-will-challenge-your-conventions
http://tech.groups.yahoo.com/group/testdrivendevelopment/
http://www.testdriven.com