Python: Generate Test Case Stubs For All Classes In A Module

I had a module full of classes. I wanted to generate a unittest.TestCase stub for every class, such as this:

# TODO: Finish this
class Test_className(unittest.TestCase):
    def setUp(self):
        pass

So, I wrote the following Python script:

if __name__=="__main__":
    file = "c:/dev/sourceFile.py"
    f = open(file, "r")
    line = f.readline()
    count = 0
    thing = [ ]
 
    while line:
        if line[:5] == "class":
            count += 1
            parenpos = line.find("(")
            classname = line[6:parenpos]
            thing.append('# TODO: Finish this')
            thing.append('class Test_%s(unittest.TestCase):' % classname)
            thing.append('    def setUp(self):')
            thing.append('        pass')
            thing.append('')
 
 
        line = f.readline()
 
    f.close()
 
    for item in thing:
        print item

Just a quick way to save myself some mundane typing.

Posted by: David Andrzejewski on July 9, 2009 • Tags: , , • Posted in: Jigs

One Response to “Python: Generate Test Case Stubs For All Classes In A Module”

  1. Jason Felice - July 10, 2009

    Now that’s what I’m talking about!

Comments are closed for this entry.