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

T

travislspencer

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
 
T

travislspencer

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

klass = module.__dict__[module.__name__]
tests.append(unittest.makeSuite(klass, "test"))
allTests = unittest.TestSuite(tests)
runner = unittest.TextTestRunner(verbosity=2)

runner.run(allTests)

This is still a kludge, but a different and less annoying one. It
works as long as the TestCase class and module have the same name.

Back to the drawing board...
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top