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