unittest: collecting tests from many modules?

Jorgen Grahn jgrahn-nntq at algonet.se
Sun Jun 12 06:14:50 EDT 2005


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?

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