unittest and automatically firing off tests

Tom Plunket tomas at fancy.org
Sat Jun 21 20:31:20 CEST 2003


Steven Taschuk wrote:

> > 1) Why in the name of all that is holy does unittest.main() throw
> >    regardless of tests passing?
> 
> unittest.main is not intended for use other than
>     if __name__ == '__main__':
>         unittest.main()

ok.  ;)

>     def suite(dirname):
>         """Create a TestSuite for the test files in the given directory."""
>         suite = unittest.TestSuite()
>         for filename in glob.glob(os.path.join(dirname, 'test_*.py')):
>             modname = os.path.splitext(os.path.basename(filename))[0]
>             modfile = file(filename)
>             try:
>                 mod = imp.load_module(modname, modfile, filename,
>                                       ('.py', 'r', imp.PY_SOURCE))
>             finally:
>                 modfile.close()
>             modsuite = unittest.defaultTestLoader.loadTestsFromModule(mod)
>             suite.addTest(modsuite)
>         return suite

Why do you call imp.load_module() here rather than __import__ ?
As a newbie it seems like you're doing a bit more work than is
required, so clearly I'm missing something.

> (Untested.)

Thanks.  I'll give this stuff a whirl.


-tom!




More information about the Python-list mailing list