[TIP] Using one unit test with different input parameters

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Feb 19 15:31:56 PST 2008


Robert Collins <robertc at robertcollins.net> writes:

> You might like to look at bzrlib.tests.TestScenarioApplier. In
> combination with bzrlib.tests.TestUtil.TestLoader, which extends
> unittests TestLoader, you can very easily parameterise some tests in
> a test script while preserving others.
> 
> For example, in the above example of sites, you might do:
> --test_foo.py
> ...
> def load_tests(standard_tests, module, loader):
>     """Multiply tests version and protocol consistency."""
>     applier = TestScenarioApplier()
>     applier.scenarios = [
> 	('wessex', {'primary': 'wessexfm.com', 'code': 'wx'}),
>         ('example.com', {'primary': 'example.com', 'code': 'ec'}),
>         ('example.org', {'primary': 'example.org', 'code': 'eo'}),
>         ]
>     # adapt everything
>     for test in iter_suite_tests(standard_tests):
>         result.addTests(applier.adapt(test))
>     return result
> 
> class TestSite(TestCase):
>     def test_a(self):
>         pass
>     def test_b(self):
>         pass
> ---

Looks interesting. How does the above code work with the extended
TestLoader <URL:http://www.python.org/doc/lib/testloader-objects.html>
class?

> When run, you'll have test ids like:
> test_foo.TestSite.test_a(wessex)
> test_foo.TestSite.test_a(example.com)
> test_foo.TestSite.test_a(example.org)
> test_foo.TestSite.test_b(wessex)
> test_foo.TestSite.test_b(example.com)
> test_foo.TestSite.test_b(example.org)
> 
> Which is much nicer for debugging that a for loop within a single
> test.

Agreed.

-- 
 \        "When in doubt tell the truth. It will confound your enemies |
  `\         and astound your friends."  -- Mark Twain, _Following the |
_o__)                                                         Equator_ |
Ben Finney




More information about the testing-in-python mailing list