[Tutor] Test code organization
Willi Richert
w.richert at gmx.net
Thu Mar 9 14:44:35 CET 2006
Hi,
for some time I try to find the best test code organization. I've come up with
the following solution, which I guess is not optimal. Please comment.
In the code directory there is a special tests directory, which contains all
the unit test files. There is the file alltests.py which collects all python
test files and executes them:
=====================================
import sys, unittest
sys.path.append(".")
sys.path.append("..")
TEST_DIR = "tests"
import glob
testCases = [t.split(".")[0] for t in glob.glob1(TEST_DIR, "*Tests.py")]
print "Found test case modules "+", ".join(testCases)
print
for t in testCases:
exec("from %s import %s"%(TEST_DIR, t))
def suite():
exec("suites = tuple([x.suite() for x in [%s]])"%str(",
".join(testCases)))
suite = unittest.TestSuite(suites)
return suite
if __name__ == '__main__':
suite = suite()
result = unittest.TextTestRunner(verbosity=2).run(suite)
if result.wasSuccessful():
sys.exit(0)
else:
sys.exit(1)
======================================
For every class to test I create a ClassTests.py file which contains the
following code:
Header:
======================================
import sys
sys.path.append("..")
import unittest
from Class... import *
======================================
The footer parses the available test classes:
======================================
def _testclasses():
mod = sys.modules[__name__]
return [getattr(mod, name) for name in dir(mod) if
name.endswith('TestCase')]
def suite():
return unittest.TestSuite([unittest.makeSuite(tc) for tc in
_testclasses()])
if __name__ == '__main__':
unittest.main()
======================================
What smalls badly is the sys.path.append() stuff every test file must have.
Improvements?
Thanks for any comment,
wr
--
Gruss,
wr
--
Dipl.-Inform. Willi Richert
C-LAB - Cooperative Computing & Communication Laboratory
der Universität Paderborn und Siemens
FU.323
Fürstenallee 11
D-33102 Paderborn
Tel: +49 5251 60 6120
Fax: +49 5251 60 6165
http://www.c-lab.de
More information about the Tutor
mailing list