A script to run all of my project's pyunit tests

travislspencer at gmail.com travislspencer at gmail.com
Fri Aug 19 15:36:21 EDT 2005


Hey All,

I am trying to write a script that runs all of my pyunit tests for me.
Ideally, I would like to be able to drop a new module into my
project's test subdirectory, and the testing script will pick it up
automatically.

At the moment, I have it working but it is kinda a kludge because
every TestCase must provide a function that returns a TestSuite for
all of its tests.

Here is the basic process and a stripped down version of the script:

1. Locate each `.py' file in my project's test subdirectory.
2. Import the module found in step 1.
3. Get a TestSuite of all of the tests in the module imported in step
2.
4. Add the TestSuite from step 3 to a growing list of suites.
5. Make one granddaddy TestSuite from the list of all those fetched in
   steps 1 through 4.
6. Create a runner, pass it the uber TestSuite, and run all the tests.

My scripts to do these things is as follows:

======================================================================
import os, sys, unittest
from glob import glob

projHome = os.getenv("PROJ_HOME")
sys.path.insert(0, projHome + "/tests")
tests = []

for file in glob(projHome + "/tests/*.py"):
    start = file.rfind("/") + 1
    end = file.rfind(".")
    moduleName = file[start:end]
    module = __import__(moduleName)

    tests.append(module.getTestSuite())
#-----------------------^
# The part that I'm not happy with.

allTests = unittest.TestSuite(tests)
runner = unittest.TextTestRunner(verbosity=2)

runner.run(allTests)
======================================================================

With this setup, every module has to have a `getTestSuite' function
that returns a TestSuite for the module.  This is suboptimal and
annoying.  The function looks like this is all of my TestCase
subclasses:

======================================================================
class MyTestCase(unittest.TestCase):
    pass

def getTestSuite():
    return suite

if __name__ == "__main__":
    unittest.main()
else:
    global suite

    suite = unittest.makeSuite(MyTestCase, 'test')
======================================================================

The problem is that I can't figure out how to do something similar to
the TestCases' `unittest.makeSuite(MyTestCase, 'test')' in the script
that I'm using to run all of the tests.

If anyone has any help or suggestions, I would really appreciate it.

--


Regards,

Travis Spencer




More information about the Python-list mailing list