summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Pool <mbp@sourcefrog.net>2010-10-12 15:10:49 +1100
committerMartin Pool <mbp@sourcefrog.net>2010-10-12 15:10:49 +1100
commit494fcff75bd2df7a42f14fee6d2f82f011f2da3f (patch)
tree1303d36371bc2d49c56b979e9a2828cdc491fcda
parent42e86e335d24e36be364ee7c003d47d0ccfa760b (diff)
downloadtestscenarios-git-494fcff75bd2df7a42f14fee6d2f82f011f2da3f.tar.gz
Support both python2.7 and bzr load_tests argument lists
-rw-r--r--lib/testscenarios/scenarios.py20
-rw-r--r--lib/testscenarios/tests/test_scenarios.py33
2 files changed, 37 insertions, 16 deletions
diff --git a/lib/testscenarios/scenarios.py b/lib/testscenarios/scenarios.py
index f008d50..3cf8091 100644
--- a/lib/testscenarios/scenarios.py
+++ b/lib/testscenarios/scenarios.py
@@ -80,23 +80,27 @@ def generate_scenarios(test_or_suite):
yield test
-def load_tests_apply_scenarios(loader, standard_tests, pattern):
+def load_tests_apply_scenarios(*params):
"""Multiply out all tests in a module that have scenarios.
If this is referenced by the `load_tests` attribute of a module, then
- Python2.7 or other testloaders that implement this protocol will
- automatically arrange for the scenarios to be expanded. In this case it
- is not necessary (or desirable) to subclass TestWithScenarios.
+ testloaders that implement this protocol will automatically arrange for
+ the scenarios to be expanded. In this case it is not necessary (or
+ desirable) to subclass TestWithScenarios.
- Note that this function implements the protocol used by Python2.7
- <http://docs.python.org/library/unittest.html#load-tests-protocol> which
- is different from the `load_tests` method originally used in bzr
- and some other projects <http://pad.lv/607412>.
+ Two different calling conventions for load_tests have been used, and this
+ function should support both. Python 2.7 passes (loader, standard_tests,
+ pattern), and bzr, nose and others have used (standard_tests,
+ module, loader). See <http://pad.lv/607412>.
:param loader: A TestLoader.
:param standard_test: The test objects found in this module before
multiplication.
"""
+ if getattr(params[0], 'suiteClass', None) is not None:
+ loader, standard_tests, pattern = params
+ else:
+ standard_tests, module, loader = params
result = loader.suiteClass()
result.addTests(generate_scenarios(standard_tests))
return result
diff --git a/lib/testscenarios/tests/test_scenarios.py b/lib/testscenarios/tests/test_scenarios.py
index 621cfdc..97a2a6b 100644
--- a/lib/testscenarios/tests/test_scenarios.py
+++ b/lib/testscenarios/tests/test_scenarios.py
@@ -177,20 +177,37 @@ class TestApplyScenarios(testtools.TestCase):
class TestLoadTests(testtools.TestCase):
+ class SampleTest(unittest.TestCase):
+ def test_nothing(self):
+ pass
+ scenarios = [
+ ('a', {}),
+ ('b', {}),
+ ]
+
def test_load_tests_apply_scenarios(self):
- class SampleTest(unittest.TestCase):
- def test_nothing(self):
- pass
- scenarios = [
- ('a', {}),
- ('b', {}),
- ]
suite = load_tests_apply_scenarios(
unittest.TestLoader(),
- [SampleTest('test_nothing')],
+ [self.SampleTest('test_nothing')],
None)
result_tests = list(testtools.iterate_tests(suite))
self.assertEquals(
2,
len(result_tests),
result_tests)
+
+ def test_load_tests_apply_scenarios_old_style(self):
+ """Call load_tests in the way used by pre-Python2.7 code.
+
+ See <https://bugs.launchpad.net/bzr/+bug/607412>
+ """
+ suite = load_tests_apply_scenarios(
+ [self.SampleTest('test_nothing')],
+ self.__class__.__module__,
+ unittest.TestLoader(),
+ )
+ result_tests = list(testtools.iterate_tests(suite))
+ self.assertEquals(
+ 2,
+ len(result_tests),
+ result_tests)