unittest and automatically firing off tests

Bernhard Herzog bh at intevation.de
Sat Jun 21 18:01:20 CEST 2003


jjl at pobox.com (John J. Lee) writes:

> Tom Plunket <tomas at fancy.org> writes:
> 
> [...snip use of unittest...]
> > Things I don't like about this?  You bet.
> > 
> > 1) Why in the name of all that is holy does unittest.main() throw
> >    regardless of tests passing?
> 
> I guess you mean 'raise', not 'throw'.  Does it?  I don't think it
> does.

It does. More precisely, the runTests method of TestProgram (TestProgram
is effectively unittest.main) calls sys.exit which raises SystemExit.

I think it's done that way to set the return code of the python process
so that in a shell you could use it to determine whether the tests were
executed successfully.

One solution is not use unittest.main when you want to run lots of tests
spread out over many modules. What I've been using for some time is
something like this (assumes that the current directory is the one
holding all the test modules):

    files = os.listdir(os.curdir)
    names = []
    for file in files:
        if file[:4] == "test" and file[-3:] == ".py":
            names.append(file[:-3])

    suite = unittest.defaultTestLoader.loadTestsFromNames(names)
    runner = unittest.TextTestRunner(verbosity = 2)
    result = runner.run(suite)

    sys.exit(not result.wasSuccessful())


   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/




More information about the Python-list mailing list