[Tutor] composing one test suite from two test cases

Tom Roche Tom_Roche at pobox.com
Mon Jan 11 14:53:36 CET 2010

Tom Roche Sun, Jan 10, 2010 at 10:44 PM
>> How to create a single unittest test suite class that runs all
>> methods from multiple TestCase classes?

Kent Johnson Mon, 11 Jan 2010 06:42:39 -0500
> The supported way to do this is to create a test suite. There is an
> example here:
> http://docs.python.org/library/unittest.html#organizing-test-code

Yes, I saw that, in particular

>>> unittest provides a TestLoader class that can be used to automate
>>> the process of creating a test suite and populating it with
>>> individual tests. For example,

>>> suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)

>>> will create a test suite that will run
>>> WidgetTestCase.testDefaultSize() and WidgetTestCase.testResize.
>>> TestSuite instances can be added to a TestSuite just as TestCase
>>> instances can be added to a TestSuite:

>>> suite1 = module1.TheTestSuite()
>>> suite2 = module2.TheTestSuite()
>>> alltests = unittest.TestSuite([suite1, suite2])

but I couldn't get AllTests.py to run: I kept getting errors about
"unbound method", "no such test method", class/instance problems, etc.
Thanks to your assistance, I suspect my key faults were:

* namespace: I was doing

- import PidTests
- import TallyPidTests

instead of

+ from PidTests import PidTests
+ from TallyPidTests import TallyPidTests

* not encapsulating the TestSuite-adding code in another suite()

The only thing your code was missing was the main(), but that was easy
to add. (The complete AllTests.py that runs for me follows my .sig)

> However I don't recommend this style of organizing tests. I prefer
> using nose for test discovery, it saves the work of creating all the
> aggregating suites.

I've heard of Nose and am planning to try it, but for now I thought
I'd "do the simplest thing that could possibly work."

> I think py.test also has a test discovery component. In Python 2.7
> the unittest module will finally have it's own test discovery.

> http://somethingaboutorange.com/mrl/projects/nose/0.11.1/finding_tests.html
> http://docs.python.org/dev/library/unittest.html#test-discovery

Thanks again, Tom Roche <Tom_Roche at pobox.com>---AllTests.py follows---

import unittest
from PidTests import PidTests
from TallyPidTests import TallyPidTests

class AllTests(unittest.TestCase):
  def suite():
    suite1 = unittest.TestLoader().loadTestsFromTestCase(PidTests)
    suite2 = unittest.TestLoader().loadTestsFromTestCase(TallyPidTests)
    return unittest.TestSuite([suite1, suite2])

def main():

if __name__ == '__main__':

More information about the Tutor mailing list