Unit-testing single function with large number of different inputs
Peter Otten
__peter__ at web.de
Wed Feb 18 03:22:11 EST 2004
Edvard Majakari wrote:
> The problem I described above shows probably one of the best reasons why
> each test method should test only one (type of) thing, but in my special
> case I don't find that appropriate due to aforementioned reasons. Any
> suggestions? I was wondering about creating test methods dynamically
> according to the files found in the test directories (which would solve
> all the problems), but I find it a tad too complex way of solving the
> problem.
I think dynamically creating test cases is the way to go. I would subclass
TestCase to test one file and then for every file put an instance of
MyTestCase into a TestSuite. For instance:
import unittest, os
# shared implementation
class Base(unittest.TestCase):
def __init__(self, filename):
unittest.TestCase.__init__(self)
self.filename = filename
# some pointless tests
class Good1(Base):
def runTest(self):
self.assertEqual(self.filename, self.filename.lower())
class Good2(Base):
def runTest(self):
self.assertEqual(self.filename.endswith(".py"), True)
class Bad(Base):
def runTest(self):
self.assertEqual(self.filename.endswith(".pyc"), True)
# build a test suite with (all applicable test cases)
# x (every file in a folder, optionally including subfolders)
def makeTests(classes, folder, recursive=False):
suite = unittest.TestSuite()
for path, folders, files in os.walk(folder):
for fn in files:
for cls in classes:
suite.addTest(cls(os.path.join(path, fn)))
if not recursive: break
return suite
# build the main test suite with subsuites for every folder
def makeSuite():
suite = unittest.TestSuite()
goodfolder = "/usr/local/lib/python2.3"
badfolder = "/usr/local/lib/python2.3"
suite.addTest(makeTests((Good1, Good2), goodfolder))
suite.addTest(makeTests((Bad,), badfolder))
return suite
if __name__ == "__main__":
unittest.main(defaultTest="makeSuite")
Peter
More information about the Python-list
mailing list