1 | n/a | import io |
---|
2 | n/a | import os |
---|
3 | n/a | import sys |
---|
4 | n/a | import pickle |
---|
5 | n/a | import subprocess |
---|
6 | n/a | |
---|
7 | n/a | import unittest |
---|
8 | n/a | from unittest.case import _Outcome |
---|
9 | n/a | |
---|
10 | n/a | from unittest.test.support import (LoggingResult, |
---|
11 | n/a | ResultWithNoStartTestRunStopTestRun) |
---|
12 | n/a | |
---|
13 | n/a | |
---|
14 | n/a | class TestCleanUp(unittest.TestCase): |
---|
15 | n/a | |
---|
16 | n/a | def testCleanUp(self): |
---|
17 | n/a | class TestableTest(unittest.TestCase): |
---|
18 | n/a | def testNothing(self): |
---|
19 | n/a | pass |
---|
20 | n/a | |
---|
21 | n/a | test = TestableTest('testNothing') |
---|
22 | n/a | self.assertEqual(test._cleanups, []) |
---|
23 | n/a | |
---|
24 | n/a | cleanups = [] |
---|
25 | n/a | |
---|
26 | n/a | def cleanup1(*args, **kwargs): |
---|
27 | n/a | cleanups.append((1, args, kwargs)) |
---|
28 | n/a | |
---|
29 | n/a | def cleanup2(*args, **kwargs): |
---|
30 | n/a | cleanups.append((2, args, kwargs)) |
---|
31 | n/a | |
---|
32 | n/a | test.addCleanup(cleanup1, 1, 2, 3, four='hello', five='goodbye') |
---|
33 | n/a | test.addCleanup(cleanup2) |
---|
34 | n/a | |
---|
35 | n/a | self.assertEqual(test._cleanups, |
---|
36 | n/a | [(cleanup1, (1, 2, 3), dict(four='hello', five='goodbye')), |
---|
37 | n/a | (cleanup2, (), {})]) |
---|
38 | n/a | |
---|
39 | n/a | self.assertTrue(test.doCleanups()) |
---|
40 | n/a | self.assertEqual(cleanups, [(2, (), {}), (1, (1, 2, 3), dict(four='hello', five='goodbye'))]) |
---|
41 | n/a | |
---|
42 | n/a | def testCleanUpWithErrors(self): |
---|
43 | n/a | class TestableTest(unittest.TestCase): |
---|
44 | n/a | def testNothing(self): |
---|
45 | n/a | pass |
---|
46 | n/a | |
---|
47 | n/a | test = TestableTest('testNothing') |
---|
48 | n/a | outcome = test._outcome = _Outcome() |
---|
49 | n/a | |
---|
50 | n/a | exc1 = Exception('foo') |
---|
51 | n/a | exc2 = Exception('bar') |
---|
52 | n/a | def cleanup1(): |
---|
53 | n/a | raise exc1 |
---|
54 | n/a | |
---|
55 | n/a | def cleanup2(): |
---|
56 | n/a | raise exc2 |
---|
57 | n/a | |
---|
58 | n/a | test.addCleanup(cleanup1) |
---|
59 | n/a | test.addCleanup(cleanup2) |
---|
60 | n/a | |
---|
61 | n/a | self.assertFalse(test.doCleanups()) |
---|
62 | n/a | self.assertFalse(outcome.success) |
---|
63 | n/a | |
---|
64 | n/a | ((_, (Type1, instance1, _)), |
---|
65 | n/a | (_, (Type2, instance2, _))) = reversed(outcome.errors) |
---|
66 | n/a | self.assertEqual((Type1, instance1), (Exception, exc1)) |
---|
67 | n/a | self.assertEqual((Type2, instance2), (Exception, exc2)) |
---|
68 | n/a | |
---|
69 | n/a | def testCleanupInRun(self): |
---|
70 | n/a | blowUp = False |
---|
71 | n/a | ordering = [] |
---|
72 | n/a | |
---|
73 | n/a | class TestableTest(unittest.TestCase): |
---|
74 | n/a | def setUp(self): |
---|
75 | n/a | ordering.append('setUp') |
---|
76 | n/a | if blowUp: |
---|
77 | n/a | raise Exception('foo') |
---|
78 | n/a | |
---|
79 | n/a | def testNothing(self): |
---|
80 | n/a | ordering.append('test') |
---|
81 | n/a | |
---|
82 | n/a | def tearDown(self): |
---|
83 | n/a | ordering.append('tearDown') |
---|
84 | n/a | |
---|
85 | n/a | test = TestableTest('testNothing') |
---|
86 | n/a | |
---|
87 | n/a | def cleanup1(): |
---|
88 | n/a | ordering.append('cleanup1') |
---|
89 | n/a | def cleanup2(): |
---|
90 | n/a | ordering.append('cleanup2') |
---|
91 | n/a | test.addCleanup(cleanup1) |
---|
92 | n/a | test.addCleanup(cleanup2) |
---|
93 | n/a | |
---|
94 | n/a | def success(some_test): |
---|
95 | n/a | self.assertEqual(some_test, test) |
---|
96 | n/a | ordering.append('success') |
---|
97 | n/a | |
---|
98 | n/a | result = unittest.TestResult() |
---|
99 | n/a | result.addSuccess = success |
---|
100 | n/a | |
---|
101 | n/a | test.run(result) |
---|
102 | n/a | self.assertEqual(ordering, ['setUp', 'test', 'tearDown', |
---|
103 | n/a | 'cleanup2', 'cleanup1', 'success']) |
---|
104 | n/a | |
---|
105 | n/a | blowUp = True |
---|
106 | n/a | ordering = [] |
---|
107 | n/a | test = TestableTest('testNothing') |
---|
108 | n/a | test.addCleanup(cleanup1) |
---|
109 | n/a | test.run(result) |
---|
110 | n/a | self.assertEqual(ordering, ['setUp', 'cleanup1']) |
---|
111 | n/a | |
---|
112 | n/a | def testTestCaseDebugExecutesCleanups(self): |
---|
113 | n/a | ordering = [] |
---|
114 | n/a | |
---|
115 | n/a | class TestableTest(unittest.TestCase): |
---|
116 | n/a | def setUp(self): |
---|
117 | n/a | ordering.append('setUp') |
---|
118 | n/a | self.addCleanup(cleanup1) |
---|
119 | n/a | |
---|
120 | n/a | def testNothing(self): |
---|
121 | n/a | ordering.append('test') |
---|
122 | n/a | |
---|
123 | n/a | def tearDown(self): |
---|
124 | n/a | ordering.append('tearDown') |
---|
125 | n/a | |
---|
126 | n/a | test = TestableTest('testNothing') |
---|
127 | n/a | |
---|
128 | n/a | def cleanup1(): |
---|
129 | n/a | ordering.append('cleanup1') |
---|
130 | n/a | test.addCleanup(cleanup2) |
---|
131 | n/a | def cleanup2(): |
---|
132 | n/a | ordering.append('cleanup2') |
---|
133 | n/a | |
---|
134 | n/a | test.debug() |
---|
135 | n/a | self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup1', 'cleanup2']) |
---|
136 | n/a | |
---|
137 | n/a | |
---|
138 | n/a | class Test_TextTestRunner(unittest.TestCase): |
---|
139 | n/a | """Tests for TextTestRunner.""" |
---|
140 | n/a | |
---|
141 | n/a | def setUp(self): |
---|
142 | n/a | # clean the environment from pre-existing PYTHONWARNINGS to make |
---|
143 | n/a | # test_warnings results consistent |
---|
144 | n/a | self.pythonwarnings = os.environ.get('PYTHONWARNINGS') |
---|
145 | n/a | if self.pythonwarnings: |
---|
146 | n/a | del os.environ['PYTHONWARNINGS'] |
---|
147 | n/a | |
---|
148 | n/a | def tearDown(self): |
---|
149 | n/a | # bring back pre-existing PYTHONWARNINGS if present |
---|
150 | n/a | if self.pythonwarnings: |
---|
151 | n/a | os.environ['PYTHONWARNINGS'] = self.pythonwarnings |
---|
152 | n/a | |
---|
153 | n/a | def test_init(self): |
---|
154 | n/a | runner = unittest.TextTestRunner() |
---|
155 | n/a | self.assertFalse(runner.failfast) |
---|
156 | n/a | self.assertFalse(runner.buffer) |
---|
157 | n/a | self.assertEqual(runner.verbosity, 1) |
---|
158 | n/a | self.assertEqual(runner.warnings, None) |
---|
159 | n/a | self.assertTrue(runner.descriptions) |
---|
160 | n/a | self.assertEqual(runner.resultclass, unittest.TextTestResult) |
---|
161 | n/a | self.assertFalse(runner.tb_locals) |
---|
162 | n/a | |
---|
163 | n/a | def test_multiple_inheritance(self): |
---|
164 | n/a | class AResult(unittest.TestResult): |
---|
165 | n/a | def __init__(self, stream, descriptions, verbosity): |
---|
166 | n/a | super(AResult, self).__init__(stream, descriptions, verbosity) |
---|
167 | n/a | |
---|
168 | n/a | class ATextResult(unittest.TextTestResult, AResult): |
---|
169 | n/a | pass |
---|
170 | n/a | |
---|
171 | n/a | # This used to raise an exception due to TextTestResult not passing |
---|
172 | n/a | # on arguments in its __init__ super call |
---|
173 | n/a | ATextResult(None, None, 1) |
---|
174 | n/a | |
---|
175 | n/a | def testBufferAndFailfast(self): |
---|
176 | n/a | class Test(unittest.TestCase): |
---|
177 | n/a | def testFoo(self): |
---|
178 | n/a | pass |
---|
179 | n/a | result = unittest.TestResult() |
---|
180 | n/a | runner = unittest.TextTestRunner(stream=io.StringIO(), failfast=True, |
---|
181 | n/a | buffer=True) |
---|
182 | n/a | # Use our result object |
---|
183 | n/a | runner._makeResult = lambda: result |
---|
184 | n/a | runner.run(Test('testFoo')) |
---|
185 | n/a | |
---|
186 | n/a | self.assertTrue(result.failfast) |
---|
187 | n/a | self.assertTrue(result.buffer) |
---|
188 | n/a | |
---|
189 | n/a | def test_locals(self): |
---|
190 | n/a | runner = unittest.TextTestRunner(stream=io.StringIO(), tb_locals=True) |
---|
191 | n/a | result = runner.run(unittest.TestSuite()) |
---|
192 | n/a | self.assertEqual(True, result.tb_locals) |
---|
193 | n/a | |
---|
194 | n/a | def testRunnerRegistersResult(self): |
---|
195 | n/a | class Test(unittest.TestCase): |
---|
196 | n/a | def testFoo(self): |
---|
197 | n/a | pass |
---|
198 | n/a | originalRegisterResult = unittest.runner.registerResult |
---|
199 | n/a | def cleanup(): |
---|
200 | n/a | unittest.runner.registerResult = originalRegisterResult |
---|
201 | n/a | self.addCleanup(cleanup) |
---|
202 | n/a | |
---|
203 | n/a | result = unittest.TestResult() |
---|
204 | n/a | runner = unittest.TextTestRunner(stream=io.StringIO()) |
---|
205 | n/a | # Use our result object |
---|
206 | n/a | runner._makeResult = lambda: result |
---|
207 | n/a | |
---|
208 | n/a | self.wasRegistered = 0 |
---|
209 | n/a | def fakeRegisterResult(thisResult): |
---|
210 | n/a | self.wasRegistered += 1 |
---|
211 | n/a | self.assertEqual(thisResult, result) |
---|
212 | n/a | unittest.runner.registerResult = fakeRegisterResult |
---|
213 | n/a | |
---|
214 | n/a | runner.run(unittest.TestSuite()) |
---|
215 | n/a | self.assertEqual(self.wasRegistered, 1) |
---|
216 | n/a | |
---|
217 | n/a | def test_works_with_result_without_startTestRun_stopTestRun(self): |
---|
218 | n/a | class OldTextResult(ResultWithNoStartTestRunStopTestRun): |
---|
219 | n/a | separator2 = '' |
---|
220 | n/a | def printErrors(self): |
---|
221 | n/a | pass |
---|
222 | n/a | |
---|
223 | n/a | class Runner(unittest.TextTestRunner): |
---|
224 | n/a | def __init__(self): |
---|
225 | n/a | super(Runner, self).__init__(io.StringIO()) |
---|
226 | n/a | |
---|
227 | n/a | def _makeResult(self): |
---|
228 | n/a | return OldTextResult() |
---|
229 | n/a | |
---|
230 | n/a | runner = Runner() |
---|
231 | n/a | runner.run(unittest.TestSuite()) |
---|
232 | n/a | |
---|
233 | n/a | def test_startTestRun_stopTestRun_called(self): |
---|
234 | n/a | class LoggingTextResult(LoggingResult): |
---|
235 | n/a | separator2 = '' |
---|
236 | n/a | def printErrors(self): |
---|
237 | n/a | pass |
---|
238 | n/a | |
---|
239 | n/a | class LoggingRunner(unittest.TextTestRunner): |
---|
240 | n/a | def __init__(self, events): |
---|
241 | n/a | super(LoggingRunner, self).__init__(io.StringIO()) |
---|
242 | n/a | self._events = events |
---|
243 | n/a | |
---|
244 | n/a | def _makeResult(self): |
---|
245 | n/a | return LoggingTextResult(self._events) |
---|
246 | n/a | |
---|
247 | n/a | events = [] |
---|
248 | n/a | runner = LoggingRunner(events) |
---|
249 | n/a | runner.run(unittest.TestSuite()) |
---|
250 | n/a | expected = ['startTestRun', 'stopTestRun'] |
---|
251 | n/a | self.assertEqual(events, expected) |
---|
252 | n/a | |
---|
253 | n/a | def test_pickle_unpickle(self): |
---|
254 | n/a | # Issue #7197: a TextTestRunner should be (un)pickleable. This is |
---|
255 | n/a | # required by test_multiprocessing under Windows (in verbose mode). |
---|
256 | n/a | stream = io.StringIO("foo") |
---|
257 | n/a | runner = unittest.TextTestRunner(stream) |
---|
258 | n/a | for protocol in range(2, pickle.HIGHEST_PROTOCOL + 1): |
---|
259 | n/a | s = pickle.dumps(runner, protocol) |
---|
260 | n/a | obj = pickle.loads(s) |
---|
261 | n/a | # StringIO objects never compare equal, a cheap test instead. |
---|
262 | n/a | self.assertEqual(obj.stream.getvalue(), stream.getvalue()) |
---|
263 | n/a | |
---|
264 | n/a | def test_resultclass(self): |
---|
265 | n/a | def MockResultClass(*args): |
---|
266 | n/a | return args |
---|
267 | n/a | STREAM = object() |
---|
268 | n/a | DESCRIPTIONS = object() |
---|
269 | n/a | VERBOSITY = object() |
---|
270 | n/a | runner = unittest.TextTestRunner(STREAM, DESCRIPTIONS, VERBOSITY, |
---|
271 | n/a | resultclass=MockResultClass) |
---|
272 | n/a | self.assertEqual(runner.resultclass, MockResultClass) |
---|
273 | n/a | |
---|
274 | n/a | expectedresult = (runner.stream, DESCRIPTIONS, VERBOSITY) |
---|
275 | n/a | self.assertEqual(runner._makeResult(), expectedresult) |
---|
276 | n/a | |
---|
277 | n/a | def test_warnings(self): |
---|
278 | n/a | """ |
---|
279 | n/a | Check that warnings argument of TextTestRunner correctly affects the |
---|
280 | n/a | behavior of the warnings. |
---|
281 | n/a | """ |
---|
282 | n/a | # see #10535 and the _test_warnings file for more information |
---|
283 | n/a | |
---|
284 | n/a | def get_parse_out_err(p): |
---|
285 | n/a | return [b.splitlines() for b in p.communicate()] |
---|
286 | n/a | opts = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
---|
287 | n/a | cwd=os.path.dirname(__file__)) |
---|
288 | n/a | ae_msg = b'Please use assertEqual instead.' |
---|
289 | n/a | at_msg = b'Please use assertTrue instead.' |
---|
290 | n/a | |
---|
291 | n/a | # no args -> all the warnings are printed, unittest warnings only once |
---|
292 | n/a | p = subprocess.Popen([sys.executable, '_test_warnings.py'], **opts) |
---|
293 | n/a | with p: |
---|
294 | n/a | out, err = get_parse_out_err(p) |
---|
295 | n/a | self.assertIn(b'OK', err) |
---|
296 | n/a | # check that the total number of warnings in the output is correct |
---|
297 | n/a | self.assertEqual(len(out), 12) |
---|
298 | n/a | # check that the numbers of the different kind of warnings is correct |
---|
299 | n/a | for msg in [b'dw', b'iw', b'uw']: |
---|
300 | n/a | self.assertEqual(out.count(msg), 3) |
---|
301 | n/a | for msg in [ae_msg, at_msg, b'rw']: |
---|
302 | n/a | self.assertEqual(out.count(msg), 1) |
---|
303 | n/a | |
---|
304 | n/a | args_list = ( |
---|
305 | n/a | # passing 'ignore' as warnings arg -> no warnings |
---|
306 | n/a | [sys.executable, '_test_warnings.py', 'ignore'], |
---|
307 | n/a | # -W doesn't affect the result if the arg is passed |
---|
308 | n/a | [sys.executable, '-Wa', '_test_warnings.py', 'ignore'], |
---|
309 | n/a | # -W affects the result if the arg is not passed |
---|
310 | n/a | [sys.executable, '-Wi', '_test_warnings.py'] |
---|
311 | n/a | ) |
---|
312 | n/a | # in all these cases no warnings are printed |
---|
313 | n/a | for args in args_list: |
---|
314 | n/a | p = subprocess.Popen(args, **opts) |
---|
315 | n/a | with p: |
---|
316 | n/a | out, err = get_parse_out_err(p) |
---|
317 | n/a | self.assertIn(b'OK', err) |
---|
318 | n/a | self.assertEqual(len(out), 0) |
---|
319 | n/a | |
---|
320 | n/a | |
---|
321 | n/a | # passing 'always' as warnings arg -> all the warnings printed, |
---|
322 | n/a | # unittest warnings only once |
---|
323 | n/a | p = subprocess.Popen([sys.executable, '_test_warnings.py', 'always'], |
---|
324 | n/a | **opts) |
---|
325 | n/a | with p: |
---|
326 | n/a | out, err = get_parse_out_err(p) |
---|
327 | n/a | self.assertIn(b'OK', err) |
---|
328 | n/a | self.assertEqual(len(out), 14) |
---|
329 | n/a | for msg in [b'dw', b'iw', b'uw', b'rw']: |
---|
330 | n/a | self.assertEqual(out.count(msg), 3) |
---|
331 | n/a | for msg in [ae_msg, at_msg]: |
---|
332 | n/a | self.assertEqual(out.count(msg), 1) |
---|
333 | n/a | |
---|
334 | n/a | def testStdErrLookedUpAtInstantiationTime(self): |
---|
335 | n/a | # see issue 10786 |
---|
336 | n/a | old_stderr = sys.stderr |
---|
337 | n/a | f = io.StringIO() |
---|
338 | n/a | sys.stderr = f |
---|
339 | n/a | try: |
---|
340 | n/a | runner = unittest.TextTestRunner() |
---|
341 | n/a | self.assertTrue(runner.stream.stream is f) |
---|
342 | n/a | finally: |
---|
343 | n/a | sys.stderr = old_stderr |
---|
344 | n/a | |
---|
345 | n/a | def testSpecifiedStreamUsed(self): |
---|
346 | n/a | # see issue 10786 |
---|
347 | n/a | f = io.StringIO() |
---|
348 | n/a | runner = unittest.TextTestRunner(f) |
---|
349 | n/a | self.assertTrue(runner.stream.stream is f) |
---|
350 | n/a | |
---|
351 | n/a | |
---|
352 | n/a | if __name__ == "__main__": |
---|
353 | n/a | unittest.main() |
---|