summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Pool <mbp@sourcefrog.net>2010-10-12 15:05:41 +1100
committerMartin Pool <mbp@sourcefrog.net>2010-10-12 15:05:41 +1100
commit42e86e335d24e36be364ee7c003d47d0ccfa760b (patch)
tree70f08ce284d8b5f2750c00def2769572d006e27e
parent553e0a70a190ab8fa81348532fb32b118ea6592f (diff)
downloadtestscenarios-git-42e86e335d24e36be364ee7c003d47d0ccfa760b.tar.gz
Add load_tests_apply_scenarios
-rw-r--r--README13
-rw-r--r--lib/testscenarios/scenarios.py24
-rw-r--r--lib/testscenarios/tests/test_scenarios.py23
3 files changed, 60 insertions, 0 deletions
diff --git a/README b/README
index b827cb6..14df2e1 100644
--- a/README
+++ b/README
@@ -128,6 +128,19 @@ With ``load_tests``::
... result.addTests(generate_scenarios(standard_tests))
... return result
+as a convenience, this is available in ``load_tests_apply_scenarios``, so a
+module using scenario tests need only say ::
+
+ from testscenarios import load_tests_apply_scenarios
+
+ load_tests = load_tests_apply_scenarios
+
+It's suggested for clarity this be done near the top of the file.
+
+Python 2.7 and greater support a different calling convention for `load_tests``
+<https://bugs.launchpad.net/bzr/+bug/607412>. `load_tests_apply_scenarios`
+copes with both.
+
With ``test_suite``::
>>> def test_suite():
diff --git a/lib/testscenarios/scenarios.py b/lib/testscenarios/scenarios.py
index e531b2e..f008d50 100644
--- a/lib/testscenarios/scenarios.py
+++ b/lib/testscenarios/scenarios.py
@@ -2,6 +2,7 @@
# dependency injection ('scenarios') by tests.
#
# Copyright (c) 2009, Robert Collins <robertc@robertcollins.net>
+# Copyright (c) 2010 Martin Pool <mbp@sourcefrog.net>
#
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
# license at the users choice. A copy of both licenses are available in the
@@ -18,6 +19,7 @@ __all__ = [
'apply_scenario',
'apply_scenarios',
'generate_scenarios',
+ 'load_tests_apply_scenarios',
]
import unittest
@@ -76,3 +78,25 @@ def generate_scenarios(test_or_suite):
yield newtest
else:
yield test
+
+
+def load_tests_apply_scenarios(loader, standard_tests, pattern):
+ """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.
+
+ 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>.
+
+ :param loader: A TestLoader.
+ :param standard_test: The test objects found in this module before
+ multiplication.
+ """
+ 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 4c80150..621cfdc 100644
--- a/lib/testscenarios/tests/test_scenarios.py
+++ b/lib/testscenarios/tests/test_scenarios.py
@@ -2,6 +2,7 @@
# dependency injection ('scenarios') by tests.
#
# Copyright (c) 2009, Robert Collins <robertc@robertcollins.net>
+# Copyright (c) 2010 Martin Pool <mbp@sourcefrog.net>
#
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
# license at the users choice. A copy of both licenses are available in the
@@ -21,6 +22,7 @@ from testscenarios.scenarios import (
apply_scenario,
apply_scenarios,
generate_scenarios,
+ load_tests_apply_scenarios,
)
import testtools
from testtools.tests.helpers import LoggingResult
@@ -171,3 +173,24 @@ class TestApplyScenarios(testtools.TestCase):
tests = list(apply_scenarios(ReferenceTest.scenarios, test))
self.assertEqual([('demo', {})], ReferenceTest.scenarios)
self.assertEqual(ReferenceTest.scenarios, tests[0].scenarios)
+
+
+class TestLoadTests(testtools.TestCase):
+
+ 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')],
+ None)
+ result_tests = list(testtools.iterate_tests(suite))
+ self.assertEquals(
+ 2,
+ len(result_tests),
+ result_tests)