Organising unit tests

jimburton jimburton1 at gmail.com
Fri Sep 29 08:51:50 EDT 2006


OK, so I'm trying to collect the tests with python and add them to the
test suite dynamically, but I have a problem with module names. Here's
what I've got:
#########################
import os
from os.path import join
import unittest

alltests = unittest.TestSuite()

def mightBeATest(f):
    #TODO check whether it really is a test
    return f.endswith('.py') and f != '__init__.py'

def isModule(d):
    if not os.path.isdir(d):
        return False
    for f in os.listdir(d):
        if f == '__init__.py':
              return True
    return False


def getTestsFromDir(dir, currentmod):
    for f in os.listdir(dir):
        if not f.startswith('.'):
            if isModule(f):
                #TODO how to check whether we are several modules in?
                mod = __import__(f)
                getTestsFromDir(os.path.join(dir, f), mod)
            elif mightBeATest(os.path.join(dir, f)):
                fname, etx = os.path.splitext(f)
                print 'adding test
with',('alltests.addTests(unittest.TestLoader().loadTestsFromTestCase('+(currentmod.__dict__[fname])+'))')

eval('alltests.addTests(unittest.TestLoader().loadTestsFromTestCase('+currentmod.__dict__[fname]+'))')


getTestsFromDir(os.curdir, None)
print alltests.countTestCases()
############################

it's importing and referring to the current module that I have a
problem with...it gives me a key error.




More information about the Python-list mailing list