[Tutor] composing one test suite from two test cases

Kent Johnson kent37 at tds.net
Mon Jan 11 12:42:39 CET 2010


On Sun, Jan 10, 2010 at 10:44 PM, Tom Roche <Tom_Roche at pobox.com> wrote:
>
> How to create a single unittest test suite class that runs all methods
> from multiple TestCase classes? Why I ask:
>
> I'm trying to relearn TDD and learn Python by coding a simple app.
> Currently the app has 2 simple functional classes, Pid and TallyPid,
> and 2 very simple testcases, PidTests and TallyPidTests:
>
> http://www.unc.edu/~tr/courses/ENVR400/Pid.py
> http://www.unc.edu/~tr/courses/ENVR400/PidTests.py
> http://www.unc.edu/~tr/courses/ENVR400/Tally.py
> http://www.unc.edu/~tr/courses/ENVR400/TallyPidTests.py

> Before I continue, I'd like to create a single suite, in a separate
> cfile file/module (e.g. AllTests.py), that will run both testcases
> (PidTests.py and TallyPidTests.py). Unfortunately I have not been able
> to do this! which surprises me, since creating such a suite in JUnit
> is trivial.  Can someone show me how to create this suite in python?
> (FWIW the python version=2.5.2: downlevel, I know, but the box also
> needs to run ArcGIS.)

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

In your case I think AllTests.py would look something like

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])


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 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

Kent


More information about the Tutor mailing list