4
from .support import LoggingResult, TestEquality
7
### Support code for Test_TestSuite
8
################################################################
11
class Foo(unittest.TestCase):
12
def test_1(self): pass
13
def test_2(self): pass
14
def test_3(self): pass
15
def runTest(self): pass
17
def _mk_TestSuite(*names):
18
return unittest.TestSuite(Test.Foo(n) for n in names)
20
################################################################
23
class Test_TestSuite(unittest.TestCase, TestEquality):
25
### Set up attributes needed by inherited tests
26
################################################################
28
# Used by TestEquality.test_eq
29
eq_pairs = [(unittest.TestSuite(), unittest.TestSuite()),
30
(unittest.TestSuite(), unittest.TestSuite([])),
31
(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
33
# Used by TestEquality.test_ne
34
ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1')),
35
(unittest.TestSuite([]), _mk_TestSuite('test_1')),
36
(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3')),
37
(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
39
################################################################
40
### /Set up attributes needed by inherited tests
42
### Tests for TestSuite.__init__
43
################################################################
45
# "class TestSuite([tests])"
47
# The tests iterable should be optional
48
def test_init__tests_optional(self):
49
suite = unittest.TestSuite()
51
self.assertEqual(suite.countTestCases(), 0)
53
# "class TestSuite([tests])"
55
# "If tests is given, it must be an iterable of individual test cases
56
# or other test suites that will be used to build the suite initially"
58
# TestSuite should deal with empty tests iterables by allowing the
59
# creation of an empty suite
60
def test_init__empty_tests(self):
61
suite = unittest.TestSuite([])
63
self.assertEqual(suite.countTestCases(), 0)
65
# "class TestSuite([tests])"
67
# "If tests is given, it must be an iterable of individual test cases
68
# or other test suites that will be used to build the suite initially"
70
# TestSuite should allow any iterable to provide tests
71
def test_init__tests_from_any_iterable(self):
73
yield unittest.FunctionTestCase(lambda: None)
74
yield unittest.FunctionTestCase(lambda: None)
76
suite_1 = unittest.TestSuite(tests())
77
self.assertEqual(suite_1.countTestCases(), 2)
79
suite_2 = unittest.TestSuite(suite_1)
80
self.assertEqual(suite_2.countTestCases(), 2)
82
suite_3 = unittest.TestSuite(set(suite_1))
83
self.assertEqual(suite_3.countTestCases(), 2)
85
# "class TestSuite([tests])"
87
# "If tests is given, it must be an iterable of individual test cases
88
# or other test suites that will be used to build the suite initially"
90
# Does TestSuite() also allow other TestSuite() instances to be present
91
# in the tests iterable?
92
def test_init__TestSuite_instances_in_tests(self):
94
ftc = unittest.FunctionTestCase(lambda: None)
95
yield unittest.TestSuite([ftc])
96
yield unittest.FunctionTestCase(lambda: None)
98
suite = unittest.TestSuite(tests())
99
self.assertEqual(suite.countTestCases(), 2)
101
################################################################
102
### /Tests for TestSuite.__init__
104
# Container types should support the iter protocol
106
test1 = unittest.FunctionTestCase(lambda: None)
107
test2 = unittest.FunctionTestCase(lambda: None)
108
suite = unittest.TestSuite((test1, test2))
110
self.assertEqual(list(suite), [test1, test2])
112
# "Return the number of tests represented by the this test object.
113
# ...this method is also implemented by the TestSuite class, which can
114
# return larger [greater than 1] values"
116
# Presumably an empty TestSuite returns 0?
117
def test_countTestCases_zero_simple(self):
118
suite = unittest.TestSuite()
120
self.assertEqual(suite.countTestCases(), 0)
122
# "Return the number of tests represented by the this test object.
123
# ...this method is also implemented by the TestSuite class, which can
124
# return larger [greater than 1] values"
126
# Presumably an empty TestSuite (even if it contains other empty
127
# TestSuite instances) returns 0?
128
def test_countTestCases_zero_nested(self):
129
class Test1(unittest.TestCase):
133
suite = unittest.TestSuite([unittest.TestSuite()])
135
self.assertEqual(suite.countTestCases(), 0)
137
# "Return the number of tests represented by the this test object.
138
# ...this method is also implemented by the TestSuite class, which can
139
# return larger [greater than 1] values"
140
def test_countTestCases_simple(self):
141
test1 = unittest.FunctionTestCase(lambda: None)
142
test2 = unittest.FunctionTestCase(lambda: None)
143
suite = unittest.TestSuite((test1, test2))
145
self.assertEqual(suite.countTestCases(), 2)
147
# "Return the number of tests represented by the this test object.
148
# ...this method is also implemented by the TestSuite class, which can
149
# return larger [greater than 1] values"
151
# Make sure this holds for nested TestSuite instances, too
152
def test_countTestCases_nested(self):
153
class Test1(unittest.TestCase):
154
def test1(self): pass
155
def test2(self): pass
157
test2 = unittest.FunctionTestCase(lambda: None)
158
test3 = unittest.FunctionTestCase(lambda: None)
159
child = unittest.TestSuite((Test1('test2'), test2))
160
parent = unittest.TestSuite((test3, child, Test1('test1')))
162
self.assertEqual(parent.countTestCases(), 4)
164
# "Run the tests associated with this suite, collecting the result into
165
# the test result object passed as result."
167
# And if there are no tests? What then?
168
def test_run__empty_suite(self):
170
result = LoggingResult(events)
172
suite = unittest.TestSuite()
176
self.assertEqual(events, [])
178
# "Note that unlike TestCase.run(), TestSuite.run() requires the
179
# "result object to be passed in."
180
def test_run__requires_result(self):
181
suite = unittest.TestSuite()
188
self.fail("Failed to raise TypeError")
190
# "Run the tests associated with this suite, collecting the result into
191
# the test result object passed as result."
194
result = LoggingResult(events)
196
class LoggingCase(unittest.TestCase):
197
def run(self, result):
198
events.append('run %s' % self._testMethodName)
200
def test1(self): pass
201
def test2(self): pass
203
tests = [LoggingCase('test1'), LoggingCase('test2')]
205
unittest.TestSuite(tests).run(result)
207
self.assertEqual(events, ['run test1', 'run test2'])
209
# "Add a TestCase ... to the suite"
210
def test_addTest__TestCase(self):
211
class Foo(unittest.TestCase):
215
suite = unittest.TestSuite()
219
self.assertEqual(suite.countTestCases(), 1)
220
self.assertEqual(list(suite), [test])
222
# "Add a ... TestSuite to the suite"
223
def test_addTest__TestSuite(self):
224
class Foo(unittest.TestCase):
227
suite_2 = unittest.TestSuite([Foo('test')])
229
suite = unittest.TestSuite()
230
suite.addTest(suite_2)
232
self.assertEqual(suite.countTestCases(), 1)
233
self.assertEqual(list(suite), [suite_2])
235
# "Add all the tests from an iterable of TestCase and TestSuite
236
# instances to this test suite."
238
# "This is equivalent to iterating over tests, calling addTest() for
240
def test_addTests(self):
241
class Foo(unittest.TestCase):
242
def test_1(self): pass
243
def test_2(self): pass
245
test_1 = Foo('test_1')
246
test_2 = Foo('test_2')
247
inner_suite = unittest.TestSuite([test_2])
254
suite_1 = unittest.TestSuite()
255
suite_1.addTests(gen())
257
self.assertEqual(list(suite_1), list(gen()))
259
# "This is equivalent to iterating over tests, calling addTest() for
261
suite_2 = unittest.TestSuite()
265
self.assertEqual(suite_1, suite_2)
267
# "Add all the tests from an iterable of TestCase and TestSuite
268
# instances to this test suite."
270
# What happens if it doesn't get an iterable?
271
def test_addTest__noniterable(self):
272
suite = unittest.TestSuite()
279
self.fail("Failed to raise TypeError")
281
def test_addTest__noncallable(self):
282
suite = unittest.TestSuite()
283
self.assertRaises(TypeError, suite.addTest, 5)
285
def test_addTest__casesuiteclass(self):
286
suite = unittest.TestSuite()
287
self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
288
self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
290
def test_addTests__string(self):
291
suite = unittest.TestSuite()
292
self.assertRaises(TypeError, suite.addTests, "foo")
294
def test_function_in_suite(self):
297
suite = unittest.TestSuite()
300
# when the bug is fixed this line will not crash
301
suite.run(unittest.TestResult())
305
def test_basetestsuite(self):
306
class Test(unittest.TestCase):
313
def tearDownClass(cls):
314
cls.wasTornDown = True
319
class Module(object):
324
Module.wasSetUp = True
326
def tearDownModule():
327
Module.wasTornDown = True
329
Test.__module__ = 'Module'
330
sys.modules['Module'] = Module
331
self.addCleanup(sys.modules.pop, 'Module')
333
suite = unittest.BaseTestSuite()
334
suite.addTests([Test('testPass'), Test('testFail')])
335
self.assertEqual(suite.countTestCases(), 2)
337
result = unittest.TestResult()
339
self.assertFalse(Module.wasSetUp)
340
self.assertFalse(Module.wasTornDown)
341
self.assertFalse(Test.wasSetUp)
342
self.assertFalse(Test.wasTornDown)
343
self.assertEqual(len(result.errors), 1)
344
self.assertEqual(len(result.failures), 0)
345
self.assertEqual(result.testsRun, 2)
348
def test_overriding_call(self):
349
class MySuite(unittest.TestSuite):
351
def __call__(self, *args, **kw):
353
unittest.TestSuite.__call__(self, *args, **kw)
356
result = unittest.TestResult()
357
wrapper = unittest.TestSuite()
358
wrapper.addTest(suite)
360
self.assertTrue(suite.called)
362
# reusing results should be permitted even if abominable
363
self.assertFalse(result._testRunEntered)
366
if __name__ == '__main__':