[TIP] Using one unit test with different input parameters
Robert Collins
robertc at robertcollins.net
Tue Feb 19 14:39:07 PST 2008
On Wed, 2008-02-20 at 09:18 +1100, Ben Finney wrote:
...
> class Site_Test(unittest.TestCase):
> def setUp(self):
> self.tb = get_browser()
> self.sites = {
> 'wessexfm.com': {
> 'primary': 'wessexfm.com',
> 'code': 'wx',
> },
> 'example.com': {
> 'primary': 'example.com',
> 'code': 'ec',
> },
> 'example.org': {
> 'primary': 'example.org',
> 'code': 'eo',
> },
> }
> In the test case given above, the 'sites' dict could simply be a
> module global instead, since it's not necessary to create it each
> time. If it contains anything that needs to be set up for each test
> case, keep it in the fixtures.
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
---
Naturally, we have helpers to make various bits even more pithy; but
they'd detract for the clarity of whats happening here.
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.
-Rob
--
GPG key available at: <http://www.robertcollins.net/keys.txt>.
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.idyll.org/pipermail/testing-in-python/attachments/20080220/1e72d4bf/attachment.pgp
More information about the testing-in-python
mailing list