469,336 Members | 5,643 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

Organising unit tests

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
1 1346
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.

Similar topics

6 posts views Thread by Tom Verbeure | last post: by
14 posts views Thread by | last post: by
72 posts views Thread by Jacob | last post: by
6 posts views Thread by Michael Bray | last post: by
5 posts views Thread by shuisheng | last post: by
176 posts views Thread by nw | last post: by
1 post views Thread by Richard Lewis Haggard | last post: by
6 posts views Thread by Vyacheslav Maslov | last post: by
5 posts views Thread by =?Utf-8?B?cmFuZHkxMjAw?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.