diff options
author | Robert Collins <robertc@robertcollins.net> | 2012-04-04 22:45:22 +1200 |
---|---|---|
committer | Robert Collins <robertc@robertcollins.net> | 2012-04-04 22:45:22 +1200 |
commit | 4f60358a8cd193d4c52ec04cd3dd299396434fdf (patch) | |
tree | 389bfd07c122834680cf30096f7b807c8e98049c | |
parent | 45564bb7c1ee43448c09f72097988756ff56e8fc (diff) | |
parent | 7fdcc7a68b231a01a11b7e4de0d4c4a73708d930 (diff) | |
download | testscenarios-git-4f60358a8cd193d4c52ec04cd3dd299396434fdf.tar.gz |
* ``TestWithScenarios`` is now backed by a mixin - WithScenarios - which can be
mixed into different unittest implementations more cleanly (e.g. unittest2).
(James Polley, Robert Collins)
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | lib/testscenarios/__init__.py | 5 | ||||
-rw-r--r-- | lib/testscenarios/testcase.py | 28 | ||||
-rw-r--r-- | lib/testscenarios/tests/__init__.py | 2 | ||||
-rw-r--r-- | lib/testscenarios/tests/test_testcase.py | 34 |
5 files changed, 50 insertions, 23 deletions
@@ -22,6 +22,10 @@ CHANGES: * Provide a load_tests implementation for easy use, and multiply_scenarios to create the cross product of scenarios. (Martin Pool) +* ``TestWithScenarios`` is now backed by a mixin - WithScenarios - which can be + mixed into different unittest implementations more cleanly (e.g. unittest2). + (James Polley, Robert Collins) + 0.1 ~~~ diff --git a/lib/testscenarios/__init__.py b/lib/testscenarios/__init__.py index c501525..f8cb056 100644 --- a/lib/testscenarios/__init__.py +++ b/lib/testscenarios/__init__.py @@ -42,11 +42,13 @@ __version__ = (0, 2, 0, 'final', 0) __all__ = [ 'TestWithScenarios', + 'WithScenarios', 'apply_scenario', 'apply_scenarios', 'generate_scenarios', 'load_tests_apply_scenarios', 'multiply_scenarios', + 'per_module_scenarios', ] @@ -57,8 +59,9 @@ from testscenarios.scenarios import ( generate_scenarios, load_tests_apply_scenarios, multiply_scenarios, + per_module_scenarios, ) -from testscenarios.testcase import TestWithScenarios +from testscenarios.testcase import TestWithScenarios, WithScenarios def test_suite(): diff --git a/lib/testscenarios/testcase.py b/lib/testscenarios/testcase.py index 5ec3a94..2ab50c7 100644 --- a/lib/testscenarios/testcase.py +++ b/lib/testscenarios/testcase.py @@ -16,6 +16,7 @@ __all__ = [ 'TestWithScenarios', + 'WithScenarios', ] import unittest @@ -24,16 +25,18 @@ from testtools.testcase import clone_test_with_new_id from testscenarios.scenarios import generate_scenarios -class TestWithScenarios(unittest.TestCase): - """A TestCase with support for scenarios via a scenarios attribute. - - When a test object which is an instance of TestWithScenarios is run, - and there is a non-empty scenarios attribute on the object, the test is - multiplied by the run method into one test per scenario. For this to work - reliably the TestWithScenarios.run method must not be overriden in a - subclass (or overridden compatibly with TestWithScenarios). +_doc = """ + When a test object which inherits from WithScenarios is run, and there is a + non-empty scenarios attribute on the object, the test is multiplied by the + run method into one test per scenario. For this to work reliably the + WithScenarios.run method must not be overriden in a subclass (or overridden + compatibly with WithScenarios). """ +class WithScenarios(object): + __doc__ = """A mixin for TestCase with support for declarative scenarios. + """ + _doc + def _get_scenarios(self): return getattr(self, 'scenarios', None) @@ -50,7 +53,7 @@ class TestWithScenarios(unittest.TestCase): for test in generate_scenarios(self): test.debug() else: - return super(TestWithScenarios, self).debug() + return super(WithScenarios, self).debug() def run(self, result=None): scenarios = self._get_scenarios() @@ -59,4 +62,9 @@ class TestWithScenarios(unittest.TestCase): test.run(result) return else: - return super(TestWithScenarios, self).run(result) + return super(WithScenarios, self).run(result) + + +class TestWithScenarios(WithScenarios, unittest.TestCase): + __doc__ = """Unittest TestCase with support for declarative scenarios. + """ + _doc diff --git a/lib/testscenarios/tests/__init__.py b/lib/testscenarios/tests/__init__.py index 6230212..8e243b6 100644 --- a/lib/testscenarios/tests/__init__.py +++ b/lib/testscenarios/tests/__init__.py @@ -40,4 +40,4 @@ def load_tests(standard_tests, module, loader): doctest.set_unittest_reportflags(doctest.REPORT_ONLY_FIRST_FAILURE) standard_tests.addTest( doctest.DocFileSuite("../../../README", optionflags=doctest.ELLIPSIS)) - return standard_tests + return loader.suiteClass(testscenarios.generate_scenarios(standard_tests)) diff --git a/lib/testscenarios/tests/test_testcase.py b/lib/testscenarios/tests/test_testcase.py index 6a9bbf9..74d2fe1 100644 --- a/lib/testscenarios/tests/test_testcase.py +++ b/lib/testscenarios/tests/test_testcase.py @@ -17,13 +17,25 @@ import unittest import testscenarios +import testtools from testtools.tests.helpers import LoggingResult -class TestTestWithScenarios(unittest.TestCase): +class TestTestWithScenarios(testtools.TestCase): + + scenarios = testscenarios.scenarios.per_module_scenarios( + 'impl', (('unittest', 'unittest'), ('unittest2', 'unittest2'))) + + @property + def Implementation(self): + if isinstance(self.impl, tuple): + self.skipTest('import failed - module not installed?') + class Implementation(testscenarios.WithScenarios, self.impl.TestCase): + pass + return Implementation def test_no_scenarios_no_error(self): - class ReferenceTest(testscenarios.TestWithScenarios): + class ReferenceTest(self.Implementation): def test_pass(self): pass test = ReferenceTest("test_pass") @@ -33,7 +45,7 @@ class TestTestWithScenarios(unittest.TestCase): self.assertEqual(1, result.testsRun) def test_with_one_scenario_one_run(self): - class ReferenceTest(testscenarios.TestWithScenarios): + class ReferenceTest(self.Implementation): scenarios = [('demo', {})] def test_pass(self): pass @@ -48,7 +60,7 @@ class TestTestWithScenarios(unittest.TestCase): log[0][1].id()) def test_with_two_scenarios_two_run(self): - class ReferenceTest(testscenarios.TestWithScenarios): + class ReferenceTest(self.Implementation): scenarios = [('1', {}), ('2', {})] def test_pass(self): pass @@ -66,7 +78,7 @@ class TestTestWithScenarios(unittest.TestCase): log[4][1].id()) def test_attributes_set(self): - class ReferenceTest(testscenarios.TestWithScenarios): + class ReferenceTest(self.Implementation): scenarios = [ ('1', {'foo': 1, 'bar': 2}), ('2', {'foo': 2, 'bar': 4})] @@ -80,7 +92,7 @@ class TestTestWithScenarios(unittest.TestCase): self.assertEqual(2, result.testsRun) def test_scenarios_attribute_cleared(self): - class ReferenceTest(testscenarios.TestWithScenarios): + class ReferenceTest(self.Implementation): scenarios = [ ('1', {'foo': 1, 'bar': 2}), ('2', {'foo': 2, 'bar': 4})] @@ -97,14 +109,14 @@ class TestTestWithScenarios(unittest.TestCase): self.assertEqual(None, log[4][1].scenarios) def test_countTestCases_no_scenarios(self): - class ReferenceTest(testscenarios.TestWithScenarios): + class ReferenceTest(self.Implementation): def test_check_foo(self): pass test = ReferenceTest("test_check_foo") self.assertEqual(1, test.countTestCases()) def test_countTestCases_empty_scenarios(self): - class ReferenceTest(testscenarios.TestWithScenarios): + class ReferenceTest(self.Implementation): scenarios = [] def test_check_foo(self): pass @@ -112,7 +124,7 @@ class TestTestWithScenarios(unittest.TestCase): self.assertEqual(1, test.countTestCases()) def test_countTestCases_1_scenarios(self): - class ReferenceTest(testscenarios.TestWithScenarios): + class ReferenceTest(self.Implementation): scenarios = [('1', {'foo': 1, 'bar': 2})] def test_check_foo(self): pass @@ -120,7 +132,7 @@ class TestTestWithScenarios(unittest.TestCase): self.assertEqual(1, test.countTestCases()) def test_countTestCases_2_scenarios(self): - class ReferenceTest(testscenarios.TestWithScenarios): + class ReferenceTest(self.Implementation): scenarios = [ ('1', {'foo': 1, 'bar': 2}), ('2', {'foo': 2, 'bar': 4})] @@ -131,7 +143,7 @@ class TestTestWithScenarios(unittest.TestCase): def test_debug_2_scenarios(self): log = [] - class ReferenceTest(testscenarios.TestWithScenarios): + class ReferenceTest(self.Implementation): scenarios = [ ('1', {'foo': 1, 'bar': 2}), ('2', {'foo': 2, 'bar': 4})] |