Organising unit tests

J

jimburton

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(directionalpan.TestPan)
suite1 =
unittest.TestLoader().loadTestsFromTestCase(layerorder.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.
 
J

jimburton

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top