unittest: collecting tests from many modules?

John Roth newsgroups at jhrothjr.com
Sun Jun 12 08:49:02 EDT 2005


"Jorgen Grahn" <jgrahn-nntq at algonet.se> wrote in message 
news:slrndao1p4.4m5.jgrahn-nntq at frailea.sa.invalid...
>I have a set of tests in different modules: test_foo.py, test_bar.py and so
> on. All of these use the simplest possible internal layout: a number of
> classes containing test*() methods, and the good old lines at the end:
>
>  if __name__ == "__main__":
>      unittest.main()
>
> This is great, because each of the modules are runnable, and I can select
> classes or tests to run on the commandline if I want to.  However, running
> all the tests from e.g. a Makefile is not that fun; I don't get a single
> pass/fail summary across the modules.
>
> What's the best way of creating a test.py which
> - aggregates the tests from all the test_*.py modules?
> - doesn't require me to enumerate all the test classes in test.py
>  (forcing each module to define test_foo.theSuite or someting would
>  be OK though)
> - retains the ability to select tests and verbosity (-q, -v) from the
>  command line?

I use your second point: I build a TestAll module. However,
I'm going to look at building the test suite using the TestLoader
class in the next version of PyFit. It sholdn't be all that difficult
to find all the Test*.py modules in a directory, import them and
use the TestLoader class to add them to the test suite.

Or, for that matter, to use reflection to find all the classes that
derive from TestCase and add them to the suite manually. That
has the advantage that one could then select classes according
to some parameter.

John Roth


>
> Something like:
>
>  import unittest
>  import test_foo
>  import test_bar
>
>  if __name__ == "__main__":
>      unittest.main(modules = ['test_foo',
>                               'test_bar'])
>
> Seems to me this should be possible, since all the logic for doing it /is/
> there for the local module; I'd assume there would be a way to make 
> unittest
> search additional modules for test classes.  But my head starts spinning
> when I read the source code ...
>
> /Jorgen
>
> -- 
>  // Jorgen Grahn <jgrahn@       Ph'nglui mglw'nafh Cthulhu
> \X/                algonet.se>   R'lyeh wgah'nagl fhtagn! 




More information about the Python-list mailing list