By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Organising unit tests

P: n/a
I have a number of unit tests organised hierarchically, all of which
inherit fixtures from a base class. To run these all at once I started
out using

from basic import map, directionalpan, layerorder, layervisibility,
listlayers, streamlayerlist
# ... lots more ...

suite0 =
unittest.TestLoader().loadTestsFromTestCase(direct ionalpan.TestPan)
suite1 =
unittest.TestLoader().loadTestsFromTestCase(layero rder.TestLayerorder)
# ... lots more ...

alltests = unittest.TestSuite([suite0
, suite1
....])
unittest.TextTestRunner(verbosity=2).run(alltests)

Which is unmaintainable. the TestLoader docs warn that
loadTestsFromModule will not play nicely with my situation and indeed
if I try

suite0 = unittest.TestLoader().loadTestsFromModule(basic)

then run the tests, it hasn't picked any up. I suppose I could collect
and run the tests with a shell script...what are my options here to
avoid hardcoding and maintaining the list of tests, and how is it
normally done?

Thanks.

Sep 29 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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().lo adTestsFromTestCase('+(currentmod.__dict__[fname])+'))')

eval('alltests.addTests(unittest.TestLoader().load TestsFromTestCase('+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.

Sep 29 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.