views:

172

answers:

3

I have a heirarchical folder full of Python unit-tests. They are all importable ".py" files which define TestCase objects. This folder contains thousands of files in many nested subdirectories and was written by somebody else. I do not have permission to change it, I just have to run it.

I want to generate a single TestSuite object which contains all of the TestCases in the folder. Is there an easy and elegant way to do this?

Thanks

+4  A: 

The nose application may be useful for you, either directly, or to show how to implement this.

http://code.google.com/p/python-nose/ seems to be the home page.

Basically, what you want to do is walk the source tree (os.walk), use imp.load_module to load the module, use unittest.defaultTestLoader to load the tests from the module into a TestSuite, and then use that in whatever way you need to use it.

Or at least that's approximately what I do in my custom TestRunner implementation (bzr get http://code.liw.fi/coverage-test-runner/bzr/trunk).

Lars Wirzenius
+2  A: 

Look at the unittest.TestLoader (http://www.python.org/doc/2.5.2/lib/testloader-objects.html)

And the os.walk (http://www.python.org/doc/2.5.2/lib/os-file-dir.html)

You should be able to traverse your package tree using the TestLoader to build a suite which you can then run.

Something along the lines of this.

runner = unittest.TextTestRunner()
superSuite = unittest.TestSuite()
for path, dirs, files in os.walk( 'path/to/tree' ):
    # if a CVS dir or whatever: continue
    for f in files:
        # if not a python file: continue
        suite= unittest.defaultTestLoader.loadTestsFromModule( os.path.join(path,f)
        superSuite .addTests(suite ) # OR runner.run( suite)
runner.run( superSuite )

You can either walk through the tree simply running each test (runner.run(suite)) or you can accumulate a superSuite of all individual suites and run the whole mass as a single test (runner.run( superSuite )).

You don't need to do both, but I included both sets of suggestions in the above (untested) code.

S.Lott
+1  A: 

The test directory of the Python Library source shows the way. The README file describes how to write Python Regression Tests for library modules.

The regrtest.py module starts with:

"""Regression test.

This will find all modules whose name is "test_*" in the test
directory, and run them.
gimel