Python Rocks! and other rants - Agile Development 25.5.2004
Weblog of Kent S Johnson

2004-05-25

Unit testing a complex procedure

I am working on a unit test for a complicated, multi-step procedure. Conceptually it is something like this:

def complicatedStuff(self):
  
self.step1()
  
self.step2()
  
self.step3()
  
# etc...

Ideally I would like to write tests for each step:

def test_step1(self):
  
# self.obj is the object under test
  
# set up to test step 1...

  
self.obj.step1()

  
# check that step 1 was successful...

def test_step2(self):
  
# etc...

The problem is that the setup for each step is complex. The best way to set up to test step3() is to do step1() and step2(). So I have settled for a single test method that has the same structure as complicatedStuff():

def test_complicatedStuff(self):
  
# set up for step1...

  
self.obj.step1()
  
# check that step 1 was successful...

  
self.obj.step2()
  
# check that step 2 was successful...

  
self.obj.step3()
  
# etc...

This smells. It is a clear violation of Don't Repeat Yourself - the structure of complicatedStuff() is duplicated. As a result it is fragile. If the structure of complicatedStuff() changes, test_complicatedStuff() has to change the same way. On the other hand, it works, which is worth a lot!

posted at 08:43:44    #    comment []    trackback []
May 2004
MoTuWeThFrSaSu
      1 2
3 4 5 6 7 8 9
10111213141516
17181920212223
24252627282930
31      
Apr
2004
 Jun
2004

Agile development

XML-Image Letterimage

BlogRoll

© 2004, Kent Johnson