1 | n/a | import io |
---|
2 | n/a | import sys |
---|
3 | n/a | |
---|
4 | n/a | import unittest |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | def resultFactory(*_): |
---|
8 | n/a | return unittest.TestResult() |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | class TestSetups(unittest.TestCase): |
---|
12 | n/a | |
---|
13 | n/a | def getRunner(self): |
---|
14 | n/a | return unittest.TextTestRunner(resultclass=resultFactory, |
---|
15 | n/a | stream=io.StringIO()) |
---|
16 | n/a | def runTests(self, *cases): |
---|
17 | n/a | suite = unittest.TestSuite() |
---|
18 | n/a | for case in cases: |
---|
19 | n/a | tests = unittest.defaultTestLoader.loadTestsFromTestCase(case) |
---|
20 | n/a | suite.addTests(tests) |
---|
21 | n/a | |
---|
22 | n/a | runner = self.getRunner() |
---|
23 | n/a | |
---|
24 | n/a | # creating a nested suite exposes some potential bugs |
---|
25 | n/a | realSuite = unittest.TestSuite() |
---|
26 | n/a | realSuite.addTest(suite) |
---|
27 | n/a | # adding empty suites to the end exposes potential bugs |
---|
28 | n/a | suite.addTest(unittest.TestSuite()) |
---|
29 | n/a | realSuite.addTest(unittest.TestSuite()) |
---|
30 | n/a | return runner.run(realSuite) |
---|
31 | n/a | |
---|
32 | n/a | def test_setup_class(self): |
---|
33 | n/a | class Test(unittest.TestCase): |
---|
34 | n/a | setUpCalled = 0 |
---|
35 | n/a | @classmethod |
---|
36 | n/a | def setUpClass(cls): |
---|
37 | n/a | Test.setUpCalled += 1 |
---|
38 | n/a | unittest.TestCase.setUpClass() |
---|
39 | n/a | def test_one(self): |
---|
40 | n/a | pass |
---|
41 | n/a | def test_two(self): |
---|
42 | n/a | pass |
---|
43 | n/a | |
---|
44 | n/a | result = self.runTests(Test) |
---|
45 | n/a | |
---|
46 | n/a | self.assertEqual(Test.setUpCalled, 1) |
---|
47 | n/a | self.assertEqual(result.testsRun, 2) |
---|
48 | n/a | self.assertEqual(len(result.errors), 0) |
---|
49 | n/a | |
---|
50 | n/a | def test_teardown_class(self): |
---|
51 | n/a | class Test(unittest.TestCase): |
---|
52 | n/a | tearDownCalled = 0 |
---|
53 | n/a | @classmethod |
---|
54 | n/a | def tearDownClass(cls): |
---|
55 | n/a | Test.tearDownCalled += 1 |
---|
56 | n/a | unittest.TestCase.tearDownClass() |
---|
57 | n/a | def test_one(self): |
---|
58 | n/a | pass |
---|
59 | n/a | def test_two(self): |
---|
60 | n/a | pass |
---|
61 | n/a | |
---|
62 | n/a | result = self.runTests(Test) |
---|
63 | n/a | |
---|
64 | n/a | self.assertEqual(Test.tearDownCalled, 1) |
---|
65 | n/a | self.assertEqual(result.testsRun, 2) |
---|
66 | n/a | self.assertEqual(len(result.errors), 0) |
---|
67 | n/a | |
---|
68 | n/a | def test_teardown_class_two_classes(self): |
---|
69 | n/a | class Test(unittest.TestCase): |
---|
70 | n/a | tearDownCalled = 0 |
---|
71 | n/a | @classmethod |
---|
72 | n/a | def tearDownClass(cls): |
---|
73 | n/a | Test.tearDownCalled += 1 |
---|
74 | n/a | unittest.TestCase.tearDownClass() |
---|
75 | n/a | def test_one(self): |
---|
76 | n/a | pass |
---|
77 | n/a | def test_two(self): |
---|
78 | n/a | pass |
---|
79 | n/a | |
---|
80 | n/a | class Test2(unittest.TestCase): |
---|
81 | n/a | tearDownCalled = 0 |
---|
82 | n/a | @classmethod |
---|
83 | n/a | def tearDownClass(cls): |
---|
84 | n/a | Test2.tearDownCalled += 1 |
---|
85 | n/a | unittest.TestCase.tearDownClass() |
---|
86 | n/a | def test_one(self): |
---|
87 | n/a | pass |
---|
88 | n/a | def test_two(self): |
---|
89 | n/a | pass |
---|
90 | n/a | |
---|
91 | n/a | result = self.runTests(Test, Test2) |
---|
92 | n/a | |
---|
93 | n/a | self.assertEqual(Test.tearDownCalled, 1) |
---|
94 | n/a | self.assertEqual(Test2.tearDownCalled, 1) |
---|
95 | n/a | self.assertEqual(result.testsRun, 4) |
---|
96 | n/a | self.assertEqual(len(result.errors), 0) |
---|
97 | n/a | |
---|
98 | n/a | def test_error_in_setupclass(self): |
---|
99 | n/a | class BrokenTest(unittest.TestCase): |
---|
100 | n/a | @classmethod |
---|
101 | n/a | def setUpClass(cls): |
---|
102 | n/a | raise TypeError('foo') |
---|
103 | n/a | def test_one(self): |
---|
104 | n/a | pass |
---|
105 | n/a | def test_two(self): |
---|
106 | n/a | pass |
---|
107 | n/a | |
---|
108 | n/a | result = self.runTests(BrokenTest) |
---|
109 | n/a | |
---|
110 | n/a | self.assertEqual(result.testsRun, 0) |
---|
111 | n/a | self.assertEqual(len(result.errors), 1) |
---|
112 | n/a | error, _ = result.errors[0] |
---|
113 | n/a | self.assertEqual(str(error), |
---|
114 | n/a | 'setUpClass (%s.%s)' % (__name__, BrokenTest.__qualname__)) |
---|
115 | n/a | |
---|
116 | n/a | def test_error_in_teardown_class(self): |
---|
117 | n/a | class Test(unittest.TestCase): |
---|
118 | n/a | tornDown = 0 |
---|
119 | n/a | @classmethod |
---|
120 | n/a | def tearDownClass(cls): |
---|
121 | n/a | Test.tornDown += 1 |
---|
122 | n/a | raise TypeError('foo') |
---|
123 | n/a | def test_one(self): |
---|
124 | n/a | pass |
---|
125 | n/a | def test_two(self): |
---|
126 | n/a | pass |
---|
127 | n/a | |
---|
128 | n/a | class Test2(unittest.TestCase): |
---|
129 | n/a | tornDown = 0 |
---|
130 | n/a | @classmethod |
---|
131 | n/a | def tearDownClass(cls): |
---|
132 | n/a | Test2.tornDown += 1 |
---|
133 | n/a | raise TypeError('foo') |
---|
134 | n/a | def test_one(self): |
---|
135 | n/a | pass |
---|
136 | n/a | def test_two(self): |
---|
137 | n/a | pass |
---|
138 | n/a | |
---|
139 | n/a | result = self.runTests(Test, Test2) |
---|
140 | n/a | self.assertEqual(result.testsRun, 4) |
---|
141 | n/a | self.assertEqual(len(result.errors), 2) |
---|
142 | n/a | self.assertEqual(Test.tornDown, 1) |
---|
143 | n/a | self.assertEqual(Test2.tornDown, 1) |
---|
144 | n/a | |
---|
145 | n/a | error, _ = result.errors[0] |
---|
146 | n/a | self.assertEqual(str(error), |
---|
147 | n/a | 'tearDownClass (%s.%s)' % (__name__, Test.__qualname__)) |
---|
148 | n/a | |
---|
149 | n/a | def test_class_not_torndown_when_setup_fails(self): |
---|
150 | n/a | class Test(unittest.TestCase): |
---|
151 | n/a | tornDown = False |
---|
152 | n/a | @classmethod |
---|
153 | n/a | def setUpClass(cls): |
---|
154 | n/a | raise TypeError |
---|
155 | n/a | @classmethod |
---|
156 | n/a | def tearDownClass(cls): |
---|
157 | n/a | Test.tornDown = True |
---|
158 | n/a | raise TypeError('foo') |
---|
159 | n/a | def test_one(self): |
---|
160 | n/a | pass |
---|
161 | n/a | |
---|
162 | n/a | self.runTests(Test) |
---|
163 | n/a | self.assertFalse(Test.tornDown) |
---|
164 | n/a | |
---|
165 | n/a | def test_class_not_setup_or_torndown_when_skipped(self): |
---|
166 | n/a | class Test(unittest.TestCase): |
---|
167 | n/a | classSetUp = False |
---|
168 | n/a | tornDown = False |
---|
169 | n/a | @classmethod |
---|
170 | n/a | def setUpClass(cls): |
---|
171 | n/a | Test.classSetUp = True |
---|
172 | n/a | @classmethod |
---|
173 | n/a | def tearDownClass(cls): |
---|
174 | n/a | Test.tornDown = True |
---|
175 | n/a | def test_one(self): |
---|
176 | n/a | pass |
---|
177 | n/a | |
---|
178 | n/a | Test = unittest.skip("hop")(Test) |
---|
179 | n/a | self.runTests(Test) |
---|
180 | n/a | self.assertFalse(Test.classSetUp) |
---|
181 | n/a | self.assertFalse(Test.tornDown) |
---|
182 | n/a | |
---|
183 | n/a | def test_setup_teardown_order_with_pathological_suite(self): |
---|
184 | n/a | results = [] |
---|
185 | n/a | |
---|
186 | n/a | class Module1(object): |
---|
187 | n/a | @staticmethod |
---|
188 | n/a | def setUpModule(): |
---|
189 | n/a | results.append('Module1.setUpModule') |
---|
190 | n/a | @staticmethod |
---|
191 | n/a | def tearDownModule(): |
---|
192 | n/a | results.append('Module1.tearDownModule') |
---|
193 | n/a | |
---|
194 | n/a | class Module2(object): |
---|
195 | n/a | @staticmethod |
---|
196 | n/a | def setUpModule(): |
---|
197 | n/a | results.append('Module2.setUpModule') |
---|
198 | n/a | @staticmethod |
---|
199 | n/a | def tearDownModule(): |
---|
200 | n/a | results.append('Module2.tearDownModule') |
---|
201 | n/a | |
---|
202 | n/a | class Test1(unittest.TestCase): |
---|
203 | n/a | @classmethod |
---|
204 | n/a | def setUpClass(cls): |
---|
205 | n/a | results.append('setup 1') |
---|
206 | n/a | @classmethod |
---|
207 | n/a | def tearDownClass(cls): |
---|
208 | n/a | results.append('teardown 1') |
---|
209 | n/a | def testOne(self): |
---|
210 | n/a | results.append('Test1.testOne') |
---|
211 | n/a | def testTwo(self): |
---|
212 | n/a | results.append('Test1.testTwo') |
---|
213 | n/a | |
---|
214 | n/a | class Test2(unittest.TestCase): |
---|
215 | n/a | @classmethod |
---|
216 | n/a | def setUpClass(cls): |
---|
217 | n/a | results.append('setup 2') |
---|
218 | n/a | @classmethod |
---|
219 | n/a | def tearDownClass(cls): |
---|
220 | n/a | results.append('teardown 2') |
---|
221 | n/a | def testOne(self): |
---|
222 | n/a | results.append('Test2.testOne') |
---|
223 | n/a | def testTwo(self): |
---|
224 | n/a | results.append('Test2.testTwo') |
---|
225 | n/a | |
---|
226 | n/a | class Test3(unittest.TestCase): |
---|
227 | n/a | @classmethod |
---|
228 | n/a | def setUpClass(cls): |
---|
229 | n/a | results.append('setup 3') |
---|
230 | n/a | @classmethod |
---|
231 | n/a | def tearDownClass(cls): |
---|
232 | n/a | results.append('teardown 3') |
---|
233 | n/a | def testOne(self): |
---|
234 | n/a | results.append('Test3.testOne') |
---|
235 | n/a | def testTwo(self): |
---|
236 | n/a | results.append('Test3.testTwo') |
---|
237 | n/a | |
---|
238 | n/a | Test1.__module__ = Test2.__module__ = 'Module' |
---|
239 | n/a | Test3.__module__ = 'Module2' |
---|
240 | n/a | sys.modules['Module'] = Module1 |
---|
241 | n/a | sys.modules['Module2'] = Module2 |
---|
242 | n/a | |
---|
243 | n/a | first = unittest.TestSuite((Test1('testOne'),)) |
---|
244 | n/a | second = unittest.TestSuite((Test1('testTwo'),)) |
---|
245 | n/a | third = unittest.TestSuite((Test2('testOne'),)) |
---|
246 | n/a | fourth = unittest.TestSuite((Test2('testTwo'),)) |
---|
247 | n/a | fifth = unittest.TestSuite((Test3('testOne'),)) |
---|
248 | n/a | sixth = unittest.TestSuite((Test3('testTwo'),)) |
---|
249 | n/a | suite = unittest.TestSuite((first, second, third, fourth, fifth, sixth)) |
---|
250 | n/a | |
---|
251 | n/a | runner = self.getRunner() |
---|
252 | n/a | result = runner.run(suite) |
---|
253 | n/a | self.assertEqual(result.testsRun, 6) |
---|
254 | n/a | self.assertEqual(len(result.errors), 0) |
---|
255 | n/a | |
---|
256 | n/a | self.assertEqual(results, |
---|
257 | n/a | ['Module1.setUpModule', 'setup 1', |
---|
258 | n/a | 'Test1.testOne', 'Test1.testTwo', 'teardown 1', |
---|
259 | n/a | 'setup 2', 'Test2.testOne', 'Test2.testTwo', |
---|
260 | n/a | 'teardown 2', 'Module1.tearDownModule', |
---|
261 | n/a | 'Module2.setUpModule', 'setup 3', |
---|
262 | n/a | 'Test3.testOne', 'Test3.testTwo', |
---|
263 | n/a | 'teardown 3', 'Module2.tearDownModule']) |
---|
264 | n/a | |
---|
265 | n/a | def test_setup_module(self): |
---|
266 | n/a | class Module(object): |
---|
267 | n/a | moduleSetup = 0 |
---|
268 | n/a | @staticmethod |
---|
269 | n/a | def setUpModule(): |
---|
270 | n/a | Module.moduleSetup += 1 |
---|
271 | n/a | |
---|
272 | n/a | class Test(unittest.TestCase): |
---|
273 | n/a | def test_one(self): |
---|
274 | n/a | pass |
---|
275 | n/a | def test_two(self): |
---|
276 | n/a | pass |
---|
277 | n/a | Test.__module__ = 'Module' |
---|
278 | n/a | sys.modules['Module'] = Module |
---|
279 | n/a | |
---|
280 | n/a | result = self.runTests(Test) |
---|
281 | n/a | self.assertEqual(Module.moduleSetup, 1) |
---|
282 | n/a | self.assertEqual(result.testsRun, 2) |
---|
283 | n/a | self.assertEqual(len(result.errors), 0) |
---|
284 | n/a | |
---|
285 | n/a | def test_error_in_setup_module(self): |
---|
286 | n/a | class Module(object): |
---|
287 | n/a | moduleSetup = 0 |
---|
288 | n/a | moduleTornDown = 0 |
---|
289 | n/a | @staticmethod |
---|
290 | n/a | def setUpModule(): |
---|
291 | n/a | Module.moduleSetup += 1 |
---|
292 | n/a | raise TypeError('foo') |
---|
293 | n/a | @staticmethod |
---|
294 | n/a | def tearDownModule(): |
---|
295 | n/a | Module.moduleTornDown += 1 |
---|
296 | n/a | |
---|
297 | n/a | class Test(unittest.TestCase): |
---|
298 | n/a | classSetUp = False |
---|
299 | n/a | classTornDown = False |
---|
300 | n/a | @classmethod |
---|
301 | n/a | def setUpClass(cls): |
---|
302 | n/a | Test.classSetUp = True |
---|
303 | n/a | @classmethod |
---|
304 | n/a | def tearDownClass(cls): |
---|
305 | n/a | Test.classTornDown = True |
---|
306 | n/a | def test_one(self): |
---|
307 | n/a | pass |
---|
308 | n/a | def test_two(self): |
---|
309 | n/a | pass |
---|
310 | n/a | |
---|
311 | n/a | class Test2(unittest.TestCase): |
---|
312 | n/a | def test_one(self): |
---|
313 | n/a | pass |
---|
314 | n/a | def test_two(self): |
---|
315 | n/a | pass |
---|
316 | n/a | Test.__module__ = 'Module' |
---|
317 | n/a | Test2.__module__ = 'Module' |
---|
318 | n/a | sys.modules['Module'] = Module |
---|
319 | n/a | |
---|
320 | n/a | result = self.runTests(Test, Test2) |
---|
321 | n/a | self.assertEqual(Module.moduleSetup, 1) |
---|
322 | n/a | self.assertEqual(Module.moduleTornDown, 0) |
---|
323 | n/a | self.assertEqual(result.testsRun, 0) |
---|
324 | n/a | self.assertFalse(Test.classSetUp) |
---|
325 | n/a | self.assertFalse(Test.classTornDown) |
---|
326 | n/a | self.assertEqual(len(result.errors), 1) |
---|
327 | n/a | error, _ = result.errors[0] |
---|
328 | n/a | self.assertEqual(str(error), 'setUpModule (Module)') |
---|
329 | n/a | |
---|
330 | n/a | def test_testcase_with_missing_module(self): |
---|
331 | n/a | class Test(unittest.TestCase): |
---|
332 | n/a | def test_one(self): |
---|
333 | n/a | pass |
---|
334 | n/a | def test_two(self): |
---|
335 | n/a | pass |
---|
336 | n/a | Test.__module__ = 'Module' |
---|
337 | n/a | sys.modules.pop('Module', None) |
---|
338 | n/a | |
---|
339 | n/a | result = self.runTests(Test) |
---|
340 | n/a | self.assertEqual(result.testsRun, 2) |
---|
341 | n/a | |
---|
342 | n/a | def test_teardown_module(self): |
---|
343 | n/a | class Module(object): |
---|
344 | n/a | moduleTornDown = 0 |
---|
345 | n/a | @staticmethod |
---|
346 | n/a | def tearDownModule(): |
---|
347 | n/a | Module.moduleTornDown += 1 |
---|
348 | n/a | |
---|
349 | n/a | class Test(unittest.TestCase): |
---|
350 | n/a | def test_one(self): |
---|
351 | n/a | pass |
---|
352 | n/a | def test_two(self): |
---|
353 | n/a | pass |
---|
354 | n/a | Test.__module__ = 'Module' |
---|
355 | n/a | sys.modules['Module'] = Module |
---|
356 | n/a | |
---|
357 | n/a | result = self.runTests(Test) |
---|
358 | n/a | self.assertEqual(Module.moduleTornDown, 1) |
---|
359 | n/a | self.assertEqual(result.testsRun, 2) |
---|
360 | n/a | self.assertEqual(len(result.errors), 0) |
---|
361 | n/a | |
---|
362 | n/a | def test_error_in_teardown_module(self): |
---|
363 | n/a | class Module(object): |
---|
364 | n/a | moduleTornDown = 0 |
---|
365 | n/a | @staticmethod |
---|
366 | n/a | def tearDownModule(): |
---|
367 | n/a | Module.moduleTornDown += 1 |
---|
368 | n/a | raise TypeError('foo') |
---|
369 | n/a | |
---|
370 | n/a | class Test(unittest.TestCase): |
---|
371 | n/a | classSetUp = False |
---|
372 | n/a | classTornDown = False |
---|
373 | n/a | @classmethod |
---|
374 | n/a | def setUpClass(cls): |
---|
375 | n/a | Test.classSetUp = True |
---|
376 | n/a | @classmethod |
---|
377 | n/a | def tearDownClass(cls): |
---|
378 | n/a | Test.classTornDown = True |
---|
379 | n/a | def test_one(self): |
---|
380 | n/a | pass |
---|
381 | n/a | def test_two(self): |
---|
382 | n/a | pass |
---|
383 | n/a | |
---|
384 | n/a | class Test2(unittest.TestCase): |
---|
385 | n/a | def test_one(self): |
---|
386 | n/a | pass |
---|
387 | n/a | def test_two(self): |
---|
388 | n/a | pass |
---|
389 | n/a | Test.__module__ = 'Module' |
---|
390 | n/a | Test2.__module__ = 'Module' |
---|
391 | n/a | sys.modules['Module'] = Module |
---|
392 | n/a | |
---|
393 | n/a | result = self.runTests(Test, Test2) |
---|
394 | n/a | self.assertEqual(Module.moduleTornDown, 1) |
---|
395 | n/a | self.assertEqual(result.testsRun, 4) |
---|
396 | n/a | self.assertTrue(Test.classSetUp) |
---|
397 | n/a | self.assertTrue(Test.classTornDown) |
---|
398 | n/a | self.assertEqual(len(result.errors), 1) |
---|
399 | n/a | error, _ = result.errors[0] |
---|
400 | n/a | self.assertEqual(str(error), 'tearDownModule (Module)') |
---|
401 | n/a | |
---|
402 | n/a | def test_skiptest_in_setupclass(self): |
---|
403 | n/a | class Test(unittest.TestCase): |
---|
404 | n/a | @classmethod |
---|
405 | n/a | def setUpClass(cls): |
---|
406 | n/a | raise unittest.SkipTest('foo') |
---|
407 | n/a | def test_one(self): |
---|
408 | n/a | pass |
---|
409 | n/a | def test_two(self): |
---|
410 | n/a | pass |
---|
411 | n/a | |
---|
412 | n/a | result = self.runTests(Test) |
---|
413 | n/a | self.assertEqual(result.testsRun, 0) |
---|
414 | n/a | self.assertEqual(len(result.errors), 0) |
---|
415 | n/a | self.assertEqual(len(result.skipped), 1) |
---|
416 | n/a | skipped = result.skipped[0][0] |
---|
417 | n/a | self.assertEqual(str(skipped), |
---|
418 | n/a | 'setUpClass (%s.%s)' % (__name__, Test.__qualname__)) |
---|
419 | n/a | |
---|
420 | n/a | def test_skiptest_in_setupmodule(self): |
---|
421 | n/a | class Test(unittest.TestCase): |
---|
422 | n/a | def test_one(self): |
---|
423 | n/a | pass |
---|
424 | n/a | def test_two(self): |
---|
425 | n/a | pass |
---|
426 | n/a | |
---|
427 | n/a | class Module(object): |
---|
428 | n/a | @staticmethod |
---|
429 | n/a | def setUpModule(): |
---|
430 | n/a | raise unittest.SkipTest('foo') |
---|
431 | n/a | |
---|
432 | n/a | Test.__module__ = 'Module' |
---|
433 | n/a | sys.modules['Module'] = Module |
---|
434 | n/a | |
---|
435 | n/a | result = self.runTests(Test) |
---|
436 | n/a | self.assertEqual(result.testsRun, 0) |
---|
437 | n/a | self.assertEqual(len(result.errors), 0) |
---|
438 | n/a | self.assertEqual(len(result.skipped), 1) |
---|
439 | n/a | skipped = result.skipped[0][0] |
---|
440 | n/a | self.assertEqual(str(skipped), 'setUpModule (Module)') |
---|
441 | n/a | |
---|
442 | n/a | def test_suite_debug_executes_setups_and_teardowns(self): |
---|
443 | n/a | ordering = [] |
---|
444 | n/a | |
---|
445 | n/a | class Module(object): |
---|
446 | n/a | @staticmethod |
---|
447 | n/a | def setUpModule(): |
---|
448 | n/a | ordering.append('setUpModule') |
---|
449 | n/a | @staticmethod |
---|
450 | n/a | def tearDownModule(): |
---|
451 | n/a | ordering.append('tearDownModule') |
---|
452 | n/a | |
---|
453 | n/a | class Test(unittest.TestCase): |
---|
454 | n/a | @classmethod |
---|
455 | n/a | def setUpClass(cls): |
---|
456 | n/a | ordering.append('setUpClass') |
---|
457 | n/a | @classmethod |
---|
458 | n/a | def tearDownClass(cls): |
---|
459 | n/a | ordering.append('tearDownClass') |
---|
460 | n/a | def test_something(self): |
---|
461 | n/a | ordering.append('test_something') |
---|
462 | n/a | |
---|
463 | n/a | Test.__module__ = 'Module' |
---|
464 | n/a | sys.modules['Module'] = Module |
---|
465 | n/a | |
---|
466 | n/a | suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test) |
---|
467 | n/a | suite.debug() |
---|
468 | n/a | expectedOrder = ['setUpModule', 'setUpClass', 'test_something', 'tearDownClass', 'tearDownModule'] |
---|
469 | n/a | self.assertEqual(ordering, expectedOrder) |
---|
470 | n/a | |
---|
471 | n/a | def test_suite_debug_propagates_exceptions(self): |
---|
472 | n/a | class Module(object): |
---|
473 | n/a | @staticmethod |
---|
474 | n/a | def setUpModule(): |
---|
475 | n/a | if phase == 0: |
---|
476 | n/a | raise Exception('setUpModule') |
---|
477 | n/a | @staticmethod |
---|
478 | n/a | def tearDownModule(): |
---|
479 | n/a | if phase == 1: |
---|
480 | n/a | raise Exception('tearDownModule') |
---|
481 | n/a | |
---|
482 | n/a | class Test(unittest.TestCase): |
---|
483 | n/a | @classmethod |
---|
484 | n/a | def setUpClass(cls): |
---|
485 | n/a | if phase == 2: |
---|
486 | n/a | raise Exception('setUpClass') |
---|
487 | n/a | @classmethod |
---|
488 | n/a | def tearDownClass(cls): |
---|
489 | n/a | if phase == 3: |
---|
490 | n/a | raise Exception('tearDownClass') |
---|
491 | n/a | def test_something(self): |
---|
492 | n/a | if phase == 4: |
---|
493 | n/a | raise Exception('test_something') |
---|
494 | n/a | |
---|
495 | n/a | Test.__module__ = 'Module' |
---|
496 | n/a | sys.modules['Module'] = Module |
---|
497 | n/a | |
---|
498 | n/a | messages = ('setUpModule', 'tearDownModule', 'setUpClass', 'tearDownClass', 'test_something') |
---|
499 | n/a | for phase, msg in enumerate(messages): |
---|
500 | n/a | _suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test) |
---|
501 | n/a | suite = unittest.TestSuite([_suite]) |
---|
502 | n/a | with self.assertRaisesRegex(Exception, msg): |
---|
503 | n/a | suite.debug() |
---|
504 | n/a | |
---|
505 | n/a | |
---|
506 | n/a | if __name__ == '__main__': |
---|
507 | n/a | unittest.main() |
---|