Merge lp:~tchaypo/testscenarios/testcase-mixin into lp:~testtools-committers/testscenarios/trunk

Proposed by James Polley
Status: Merged
Merged at revision: 20
Proposed branch: lp:~tchaypo/testscenarios/testcase-mixin
Merge into: lp:~testtools-committers/testscenarios/trunk
Diff against target: 192 lines (+141/-4)
3 files modified
lib/testscenarios/__init__.py (+2/-1)
lib/testscenarios/testcase.py (+6/-3)
lib/testscenarios/tests/test_testcase.py (+133/-0)
To merge this branch: bzr merge lp:~tchaypo/testscenarios/testcase-mixin
Reviewer Review Type Date Requested Status
Robert Collins Pending
Review via email: mp+95713@code.launchpad.net

Commit message

Rename TestWithScenarios as MixableTestWithScenarios

Define new class TestWithScenarios, which mixes MixableTestWithScenarios with unittest.TestCase

If unittest2 is importable when running tests, a new class which mixes MixableTestWithScenarios with unittest2.TestCase is created and exercised.

Description of the change

Goal: allow TestWithScenarios to be mixed with any TestCase, not just unittest.TestCase

Implementation:
Rename TestWithScenarios as MixableTestWithScenarios

Define new class TestWithScenarios, which mixes MixableTestWithScenarios with unittest.TestCase

If unittest2 is importable when running tests, a new class which mixes MixableTestWithScenarios with unittest2.TestCase is created and exercised.

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) wrote :

Ok, this is broadly fine, module the horrible horrible fact of copy in paste when in fact you want boom-tish to parameterise the tests to test with different unittest base classes.

Martin's module-parameteriser will help with that, so I'll review and merge this after that lands, which will be after testtools gets the needed support. Be a couple of days I imagine.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/testscenarios/__init__.py'
2--- lib/testscenarios/__init__.py 2010-10-12 06:57:23 +0000
3+++ lib/testscenarios/__init__.py 2012-03-03 01:41:18 +0000
4@@ -42,6 +42,7 @@
5
6 __all__ = [
7 'TestWithScenarios',
8+ 'MixableTestWithScenarios',
9 'apply_scenario',
10 'apply_scenarios',
11 'generate_scenarios',
12@@ -58,7 +59,7 @@
13 load_tests_apply_scenarios,
14 multiply_scenarios,
15 )
16-from testscenarios.testcase import TestWithScenarios
17+from testscenarios.testcase import TestWithScenarios, MixableTestWithScenarios
18
19
20 def test_suite():
21
22=== modified file 'lib/testscenarios/testcase.py'
23--- lib/testscenarios/testcase.py 2009-12-19 03:09:14 +0000
24+++ lib/testscenarios/testcase.py 2012-03-03 01:41:18 +0000
25@@ -24,7 +24,7 @@
26
27 from testscenarios.scenarios import generate_scenarios
28
29-class TestWithScenarios(unittest.TestCase):
30+class MixableTestWithScenarios(object):
31 """A TestCase with support for scenarios via a scenarios attribute.
32
33 When a test object which is an instance of TestWithScenarios is run,
34@@ -50,7 +50,7 @@
35 for test in generate_scenarios(self):
36 test.debug()
37 else:
38- return super(TestWithScenarios, self).debug()
39+ return super(MixableTestWithScenarios, self).debug()
40
41 def run(self, result=None):
42 scenarios = self._get_scenarios()
43@@ -59,4 +59,7 @@
44 test.run(result)
45 return
46 else:
47- return super(TestWithScenarios, self).run(result)
48+ return super(MixableTestWithScenarios, self).run(result)
49+
50+class TestWithScenarios(MixableTestWithScenarios, unittest.TestCase):
51+ pass
52
53=== modified file 'lib/testscenarios/tests/test_testcase.py'
54--- lib/testscenarios/tests/test_testcase.py 2009-12-19 03:09:14 +0000
55+++ lib/testscenarios/tests/test_testcase.py 2012-03-03 01:41:18 +0000
56@@ -143,3 +143,136 @@
57 self.assertEqual(None, log[0].scenarios)
58 self.assertEqual(None, log[1].scenarios)
59 self.assertNotEqual(log[0].id(), log[1].id())
60+
61+try:
62+ import unittest2
63+ class TestWithScenariosWithUnittest2(testscenarios.MixableTestWithScenarios, unittest2.TestCase):
64+ pass
65+
66+ class TestTestWithScenariosWithInittest2(unittest.TestCase):
67+
68+ def test_no_scenarios_no_error(self):
69+ class ReferenceTest(TestWithScenariosWithUnittest2):
70+ def test_pass(self):
71+ pass
72+ test = ReferenceTest("test_pass")
73+ result = unittest.TestResult()
74+ test.run(result)
75+ self.assertTrue(result.wasSuccessful())
76+ self.assertEqual(1, result.testsRun)
77+
78+ def test_with_one_scenario_one_run(self):
79+ class ReferenceTest(TestWithScenariosWithUnittest2):
80+ scenarios = [('demo', {})]
81+ def test_pass(self):
82+ pass
83+ test = ReferenceTest("test_pass")
84+ log = []
85+ result = LoggingResult(log)
86+ test.run(result)
87+ self.assertTrue(result.wasSuccessful())
88+ self.assertEqual(1, result.testsRun)
89+ self.assertEqual(
90+ 'testscenarios.tests.test_testcase.ReferenceTest.test_pass(demo)',
91+ log[0][1].id())
92+
93+ def test_with_two_scenarios_two_run(self):
94+ class ReferenceTest(TestWithScenariosWithUnittest2):
95+ scenarios = [('1', {}), ('2', {})]
96+ def test_pass(self):
97+ pass
98+ test = ReferenceTest("test_pass")
99+ log = []
100+ result = LoggingResult(log)
101+ test.run(result)
102+ self.assertTrue(result.wasSuccessful())
103+ self.assertEqual(2, result.testsRun)
104+ self.assertEqual(
105+ 'testscenarios.tests.test_testcase.ReferenceTest.test_pass(1)',
106+ log[0][1].id())
107+ self.assertEqual(
108+ 'testscenarios.tests.test_testcase.ReferenceTest.test_pass(2)',
109+ log[4][1].id())
110+
111+ def test_attributes_set(self):
112+ class ReferenceTest(TestWithScenariosWithUnittest2):
113+ scenarios = [
114+ ('1', {'foo': 1, 'bar': 2}),
115+ ('2', {'foo': 2, 'bar': 4})]
116+ def test_check_foo(self):
117+ self.assertEqual(self.foo * 2, self.bar)
118+ test = ReferenceTest("test_check_foo")
119+ log = []
120+ result = LoggingResult(log)
121+ test.run(result)
122+ self.assertTrue(result.wasSuccessful())
123+ self.assertEqual(2, result.testsRun)
124+
125+ def test_scenarios_attribute_cleared(self):
126+ class ReferenceTest(TestWithScenariosWithUnittest2):
127+ scenarios = [
128+ ('1', {'foo': 1, 'bar': 2}),
129+ ('2', {'foo': 2, 'bar': 4})]
130+ def test_check_foo(self):
131+ self.assertEqual(self.foo * 2, self.bar)
132+ test = ReferenceTest("test_check_foo")
133+ log = []
134+ result = LoggingResult(log)
135+ test.run(result)
136+ self.assertTrue(result.wasSuccessful())
137+ self.assertEqual(2, result.testsRun)
138+ self.assertNotEqual(None, test.scenarios)
139+ self.assertEqual(None, log[0][1].scenarios)
140+ self.assertEqual(None, log[4][1].scenarios)
141+
142+ def test_countTestCases_no_scenarios(self):
143+ class ReferenceTest(TestWithScenariosWithUnittest2):
144+ def test_check_foo(self):
145+ pass
146+ test = ReferenceTest("test_check_foo")
147+ self.assertEqual(1, test.countTestCases())
148+
149+ def test_countTestCases_empty_scenarios(self):
150+ class ReferenceTest(TestWithScenariosWithUnittest2):
151+ scenarios = []
152+ def test_check_foo(self):
153+ pass
154+ test = ReferenceTest("test_check_foo")
155+ self.assertEqual(1, test.countTestCases())
156+
157+ def test_countTestCases_1_scenarios(self):
158+ class ReferenceTest(TestWithScenariosWithUnittest2):
159+ scenarios = [('1', {'foo': 1, 'bar': 2})]
160+ def test_check_foo(self):
161+ pass
162+ test = ReferenceTest("test_check_foo")
163+ self.assertEqual(1, test.countTestCases())
164+
165+ def test_countTestCases_2_scenarios(self):
166+ class ReferenceTest(TestWithScenariosWithUnittest2):
167+ scenarios = [
168+ ('1', {'foo': 1, 'bar': 2}),
169+ ('2', {'foo': 2, 'bar': 4})]
170+ def test_check_foo(self):
171+ pass
172+ test = ReferenceTest("test_check_foo")
173+ self.assertEqual(2, test.countTestCases())
174+
175+ def test_debug_2_scenarios(self):
176+ log = []
177+ class ReferenceTest(TestWithScenariosWithUnittest2):
178+ scenarios = [
179+ ('1', {'foo': 1, 'bar': 2}),
180+ ('2', {'foo': 2, 'bar': 4})]
181+ def test_check_foo(self):
182+ log.append(self)
183+ test = ReferenceTest("test_check_foo")
184+ test.debug()
185+ self.assertEqual(2, len(log))
186+ self.assertEqual(None, log[0].scenarios)
187+ self.assertEqual(None, log[1].scenarios)
188+ self.assertNotEqual(log[0].id(), log[1].id())
189+
190+
191+except ImportError:
192+ pass

Subscribers

People subscribed via source and target branches