| 1 |
import io |
| 2 |
import sys |
| 3 |
import textwrap |
| 4 |
import traceback |
| 5 |
import unittest |
| 6 |
from unittest.util import strclass |
| 7 |
from test.support import warnings_helper |
| 8 |
from test.support import captured_stdout, force_not_colorized_test_class |
| 9 |
from test.test_unittest.support import BufferedWriter |
| 10 |
|
| 11 |
|
| 12 |
class MockTraceback(object): |
| 13 |
class TracebackException: |
| 14 |
def __init__(self, *args, **kwargs): |
| 15 |
self.capture_locals = kwargs.get('capture_locals', False) |
| 16 |
def format(self, **kwargs): |
| 17 |
result = ['A traceback'] |
| 18 |
if self.capture_locals: |
| 19 |
result.append('locals') |
| 20 |
return result |
| 21 |
|
| 22 |
def restore_traceback(): |
| 23 |
unittest.result.traceback = traceback |
| 24 |
|
| 25 |
|
| 26 |
def bad_cleanup1(): |
| 27 |
print('do cleanup1') |
| 28 |
raise TypeError('bad cleanup1') |
| 29 |
|
| 30 |
|
| 31 |
def bad_cleanup2(): |
| 32 |
print('do cleanup2') |
| 33 |
raise ValueError('bad cleanup2') |
| 34 |
|
| 35 |
|
| 36 |
@force_not_colorized_test_class |
| 37 |
class Test_TestResult(unittest.TestCase): |
| 38 |
# Note: there are not separate tests for TestResult.wasSuccessful(), |
| 39 |
# TestResult.errors, TestResult.failures, TestResult.testsRun or |
| 40 |
# TestResult.shouldStop because these only have meaning in terms of |
| 41 |
# other TestResult methods. |
| 42 |
# |
| 43 |
# Accordingly, tests for the aforenamed attributes are incorporated |
| 44 |
# in with the tests for the defining methods. |
| 45 |
################################################################ |
| 46 |
|
| 47 |
def test_init(self): |
| 48 |
result = unittest.TestResult() |
| 49 |
|
| 50 |
self.assertTrue(result.wasSuccessful()) |
| 51 |
self.assertEqual(len(result.errors), 0) |
| 52 |
self.assertEqual(len(result.failures), 0) |
| 53 |
self.assertEqual(result.testsRun, 0) |
| 54 |
self.assertEqual(result.shouldStop, False) |
| 55 |
self.assertIsNone(result._stdout_buffer) |
| 56 |
self.assertIsNone(result._stderr_buffer) |
| 57 |
|
| 58 |
# "This method can be called to signal that the set of tests being |
| 59 |
# run should be aborted by setting the TestResult's shouldStop |
| 60 |
# attribute to True." |
| 61 |
def test_stop(self): |
| 62 |
result = unittest.TestResult() |
| 63 |
|
| 64 |
result.stop() |
| 65 |
|
| 66 |
self.assertEqual(result.shouldStop, True) |
| 67 |
|
| 68 |
# "Called when the test case test is about to be run. The default |
| 69 |
# implementation simply increments the instance's testsRun counter." |
| 70 |
def test_startTest(self): |
| 71 |
class Foo(unittest.TestCase): |
| 72 |
def test_1(self): |
| 73 |
pass |
| 74 |
|
| 75 |
test = Foo('test_1') |
| 76 |
|
| 77 |
result = unittest.TestResult() |
| 78 |
|
| 79 |
result.startTest(test) |
| 80 |
|
| 81 |
self.assertTrue(result.wasSuccessful()) |
| 82 |
self.assertEqual(len(result.errors), 0) |
| 83 |
self.assertEqual(len(result.failures), 0) |
| 84 |
self.assertEqual(result.testsRun, 1) |
| 85 |
self.assertEqual(result.shouldStop, False) |
| 86 |
|
| 87 |
result.stopTest(test) |
| 88 |
|
| 89 |
# "Called after the test case test has been executed, regardless of |
| 90 |
# the outcome. The default implementation does nothing." |
| 91 |
def test_stopTest(self): |
| 92 |
class Foo(unittest.TestCase): |
| 93 |
def test_1(self): |
| 94 |
pass |
| 95 |
|
| 96 |
test = Foo('test_1') |
| 97 |
|
| 98 |
result = unittest.TestResult() |
| 99 |
|
| 100 |
result.startTest(test) |
| 101 |
|
| 102 |
self.assertTrue(result.wasSuccessful()) |
| 103 |
self.assertEqual(len(result.errors), 0) |
| 104 |
self.assertEqual(len(result.failures), 0) |
| 105 |
self.assertEqual(result.testsRun, 1) |
| 106 |
self.assertEqual(result.shouldStop, False) |
| 107 |
|
| 108 |
result.stopTest(test) |
| 109 |
|
| 110 |
# Same tests as above; make sure nothing has changed |
| 111 |
self.assertTrue(result.wasSuccessful()) |
| 112 |
self.assertEqual(len(result.errors), 0) |
| 113 |
self.assertEqual(len(result.failures), 0) |
| 114 |
self.assertEqual(result.testsRun, 1) |
| 115 |
self.assertEqual(result.shouldStop, False) |
| 116 |
|
| 117 |
# "Called before and after tests are run. The default implementation does nothing." |
| 118 |
def test_startTestRun_stopTestRun(self): |
| 119 |
result = unittest.TestResult() |
| 120 |
result.startTestRun() |
| 121 |
result.stopTestRun() |
| 122 |
|
| 123 |
# "addSuccess(test)" |
| 124 |
# ... |
| 125 |
# "Called when the test case test succeeds" |
| 126 |
# ... |
| 127 |
# "wasSuccessful() - Returns True if all tests run so far have passed, |
| 128 |
# otherwise returns False" |
| 129 |
# ... |
| 130 |
# "testsRun - The total number of tests run so far." |
| 131 |
# ... |
| 132 |
# "errors - A list containing 2-tuples of TestCase instances and |
| 133 |
# formatted tracebacks. Each tuple represents a test which raised an |
| 134 |
# unexpected exception. Contains formatted |
| 135 |
# tracebacks instead of sys.exc_info() results." |
| 136 |
# ... |
| 137 |
# "failures - A list containing 2-tuples of TestCase instances and |
| 138 |
# formatted tracebacks. Each tuple represents a test where a failure was |
| 139 |
# explicitly signalled using the TestCase.fail*() or TestCase.assert*() |
| 140 |
# methods. Contains formatted tracebacks instead |
| 141 |
# of sys.exc_info() results." |
| 142 |
def test_addSuccess(self): |
| 143 |
class Foo(unittest.TestCase): |
| 144 |
def test_1(self): |
| 145 |
pass |
| 146 |
|
| 147 |
test = Foo('test_1') |
| 148 |
|
| 149 |
result = unittest.TestResult() |
| 150 |
|
| 151 |
result.startTest(test) |
| 152 |
result.addSuccess(test) |
| 153 |
result.stopTest(test) |
| 154 |
|
| 155 |
self.assertTrue(result.wasSuccessful()) |
| 156 |
self.assertEqual(len(result.errors), 0) |
| 157 |
self.assertEqual(len(result.failures), 0) |
| 158 |
self.assertEqual(result.testsRun, 1) |
| 159 |
self.assertEqual(result.shouldStop, False) |
| 160 |
|
| 161 |
# "addFailure(test, err)" |
| 162 |
# ... |
| 163 |
# "Called when the test case test signals a failure. err is a tuple of |
| 164 |
# the form returned by sys.exc_info(): (type, value, traceback)" |
| 165 |
# ... |
| 166 |
# "wasSuccessful() - Returns True if all tests run so far have passed, |
| 167 |
# otherwise returns False" |
| 168 |
# ... |
| 169 |
# "testsRun - The total number of tests run so far." |
| 170 |
# ... |
| 171 |
# "errors - A list containing 2-tuples of TestCase instances and |
| 172 |
# formatted tracebacks. Each tuple represents a test which raised an |
| 173 |
# unexpected exception. Contains formatted |
| 174 |
# tracebacks instead of sys.exc_info() results." |
| 175 |
# ... |
| 176 |
# "failures - A list containing 2-tuples of TestCase instances and |
| 177 |
# formatted tracebacks. Each tuple represents a test where a failure was |
| 178 |
# explicitly signalled using the TestCase.fail*() or TestCase.assert*() |
| 179 |
# methods. Contains formatted tracebacks instead |
| 180 |
# of sys.exc_info() results." |
| 181 |
def test_addFailure(self): |
| 182 |
class Foo(unittest.TestCase): |
| 183 |
def test_1(self): |
| 184 |
pass |
| 185 |
|
| 186 |
test = Foo('test_1') |
| 187 |
try: |
| 188 |
test.fail("foo") |
| 189 |
except AssertionError: |
| 190 |
exc_info_tuple = sys.exc_info() |
| 191 |
|
| 192 |
result = unittest.TestResult() |
| 193 |
|
| 194 |
result.startTest(test) |
| 195 |
result.addFailure(test, exc_info_tuple) |
| 196 |
result.stopTest(test) |
| 197 |
|
| 198 |
self.assertFalse(result.wasSuccessful()) |
| 199 |
self.assertEqual(len(result.errors), 0) |
| 200 |
self.assertEqual(len(result.failures), 1) |
| 201 |
self.assertEqual(result.testsRun, 1) |
| 202 |
self.assertEqual(result.shouldStop, False) |
| 203 |
|
| 204 |
test_case, formatted_exc = result.failures[0] |
| 205 |
self.assertIs(test_case, test) |
| 206 |
self.assertIsInstance(formatted_exc, str) |
| 207 |
|
| 208 |
def test_addFailure_filter_traceback_frames(self): |
| 209 |
class Foo(unittest.TestCase): |
| 210 |
def test_1(self): |
| 211 |
pass |
| 212 |
|
| 213 |
test = Foo('test_1') |
| 214 |
def get_exc_info(): |
| 215 |
try: |
| 216 |
test.fail("foo") |
| 217 |
except AssertionError: |
| 218 |
return sys.exc_info() |
| 219 |
|
| 220 |
exc_info_tuple = get_exc_info() |
| 221 |
|
| 222 |
full_exc = traceback.format_exception(*exc_info_tuple) |
| 223 |
|
| 224 |
result = unittest.TestResult() |
| 225 |
result.startTest(test) |
| 226 |
result.addFailure(test, exc_info_tuple) |
| 227 |
result.stopTest(test) |
| 228 |
|
| 229 |
formatted_exc = result.failures[0][1] |
| 230 |
dropped = [l for l in full_exc if l not in formatted_exc] |
| 231 |
self.assertEqual(len(dropped), 1) |
| 232 |
self.assertIn("raise self.failureException(msg)", dropped[0]) |
| 233 |
|
| 234 |
def test_addFailure_filter_traceback_frames_context(self): |
| 235 |
class Foo(unittest.TestCase): |
| 236 |
def test_1(self): |
| 237 |
pass |
| 238 |
|
| 239 |
test = Foo('test_1') |
| 240 |
def get_exc_info(): |
| 241 |
try: |
| 242 |
try: |
| 243 |
test.fail("foo") |
| 244 |
except AssertionError: |
| 245 |
raise ValueError(42) |
| 246 |
except ValueError: |
| 247 |
return sys.exc_info() |
| 248 |
|
| 249 |
exc_info_tuple = get_exc_info() |
| 250 |
|
| 251 |
full_exc = traceback.format_exception(*exc_info_tuple) |
| 252 |
|
| 253 |
result = unittest.TestResult() |
| 254 |
result.startTest(test) |
| 255 |
result.addFailure(test, exc_info_tuple) |
| 256 |
result.stopTest(test) |
| 257 |
|
| 258 |
formatted_exc = result.failures[0][1] |
| 259 |
dropped = [l for l in full_exc if l not in formatted_exc] |
| 260 |
self.assertEqual(len(dropped), 1) |
| 261 |
self.assertIn("raise self.failureException(msg)", dropped[0]) |
| 262 |
|
| 263 |
def test_addFailure_filter_traceback_frames_chained_exception_self_loop(self): |
| 264 |
class Foo(unittest.TestCase): |
| 265 |
def test_1(self): |
| 266 |
pass |
| 267 |
|
| 268 |
def get_exc_info(): |
| 269 |
try: |
| 270 |
loop = Exception("Loop") |
| 271 |
loop.__cause__ = loop |
| 272 |
loop.__context__ = loop |
| 273 |
raise loop |
| 274 |
except Exception: |
| 275 |
return sys.exc_info() |
| 276 |
|
| 277 |
exc_info_tuple = get_exc_info() |
| 278 |
|
| 279 |
test = Foo('test_1') |
| 280 |
result = unittest.TestResult() |
| 281 |
result.startTest(test) |
| 282 |
result.addFailure(test, exc_info_tuple) |
| 283 |
result.stopTest(test) |
| 284 |
|
| 285 |
formatted_exc = result.failures[0][1] |
| 286 |
self.assertEqual(formatted_exc.count("Exception: Loop\n"), 1) |
| 287 |
|
| 288 |
def test_addFailure_filter_traceback_frames_chained_exception_cycle(self): |
| 289 |
class Foo(unittest.TestCase): |
| 290 |
def test_1(self): |
| 291 |
pass |
| 292 |
|
| 293 |
def get_exc_info(): |
| 294 |
try: |
| 295 |
# Create two directionally opposed cycles |
| 296 |
# __cause__ in one direction, __context__ in the other |
| 297 |
A, B, C = Exception("A"), Exception("B"), Exception("C") |
| 298 |
edges = [(C, B), (B, A), (A, C)] |
| 299 |
for ex1, ex2 in edges: |
| 300 |
ex1.__cause__ = ex2 |
| 301 |
ex2.__context__ = ex1 |
| 302 |
raise C |
| 303 |
except Exception: |
| 304 |
return sys.exc_info() |
| 305 |
|
| 306 |
exc_info_tuple = get_exc_info() |
| 307 |
|
| 308 |
test = Foo('test_1') |
| 309 |
result = unittest.TestResult() |
| 310 |
result.startTest(test) |
| 311 |
result.addFailure(test, exc_info_tuple) |
| 312 |
result.stopTest(test) |
| 313 |
|
| 314 |
formatted_exc = result.failures[0][1] |
| 315 |
self.assertEqual(formatted_exc.count("Exception: A\n"), 1) |
| 316 |
self.assertEqual(formatted_exc.count("Exception: B\n"), 1) |
| 317 |
self.assertEqual(formatted_exc.count("Exception: C\n"), 1) |
| 318 |
|
| 319 |
# "addError(test, err)" |
| 320 |
# ... |
| 321 |
# "Called when the test case test raises an unexpected exception err |
| 322 |
# is a tuple of the form returned by sys.exc_info(): |
| 323 |
# (type, value, traceback)" |
| 324 |
# ... |
| 325 |
# "wasSuccessful() - Returns True if all tests run so far have passed, |
| 326 |
# otherwise returns False" |
| 327 |
# ... |
| 328 |
# "testsRun - The total number of tests run so far." |
| 329 |
# ... |
| 330 |
# "errors - A list containing 2-tuples of TestCase instances and |
| 331 |
# formatted tracebacks. Each tuple represents a test which raised an |
| 332 |
# unexpected exception. Contains formatted |
| 333 |
# tracebacks instead of sys.exc_info() results." |
| 334 |
# ... |
| 335 |
# "failures - A list containing 2-tuples of TestCase instances and |
| 336 |
# formatted tracebacks. Each tuple represents a test where a failure was |
| 337 |
# explicitly signalled using the TestCase.fail*() or TestCase.assert*() |
| 338 |
# methods. Contains formatted tracebacks instead |
| 339 |
# of sys.exc_info() results." |
| 340 |
def test_addError(self): |
| 341 |
class Foo(unittest.TestCase): |
| 342 |
def test_1(self): |
| 343 |
pass |
| 344 |
|
| 345 |
test = Foo('test_1') |
| 346 |
try: |
| 347 |
raise TypeError() |
| 348 |
except TypeError: |
| 349 |
exc_info_tuple = sys.exc_info() |
| 350 |
|
| 351 |
result = unittest.TestResult() |
| 352 |
|
| 353 |
result.startTest(test) |
| 354 |
result.addError(test, exc_info_tuple) |
| 355 |
result.stopTest(test) |
| 356 |
|
| 357 |
self.assertFalse(result.wasSuccessful()) |
| 358 |
self.assertEqual(len(result.errors), 1) |
| 359 |
self.assertEqual(len(result.failures), 0) |
| 360 |
self.assertEqual(result.testsRun, 1) |
| 361 |
self.assertEqual(result.shouldStop, False) |
| 362 |
|
| 363 |
test_case, formatted_exc = result.errors[0] |
| 364 |
self.assertIs(test_case, test) |
| 365 |
self.assertIsInstance(formatted_exc, str) |
| 366 |
|
| 367 |
def test_addError_locals(self): |
| 368 |
class Foo(unittest.TestCase): |
| 369 |
def test_1(self): |
| 370 |
1/0 |
| 371 |
|
| 372 |
test = Foo('test_1') |
| 373 |
result = unittest.TestResult() |
| 374 |
result.tb_locals = True |
| 375 |
|
| 376 |
unittest.result.traceback = MockTraceback |
| 377 |
self.addCleanup(restore_traceback) |
| 378 |
result.startTestRun() |
| 379 |
test.run(result) |
| 380 |
result.stopTestRun() |
| 381 |
|
| 382 |
self.assertEqual(len(result.errors), 1) |
| 383 |
test_case, formatted_exc = result.errors[0] |
| 384 |
self.assertEqual('A tracebacklocals', formatted_exc) |
| 385 |
|
| 386 |
def test_addSubTest(self): |
| 387 |
class Foo(unittest.TestCase): |
| 388 |
def test_1(self): |
| 389 |
nonlocal subtest |
| 390 |
with self.subTest(foo=1): |
| 391 |
subtest = self._subtest |
| 392 |
try: |
| 393 |
1/0 |
| 394 |
except ZeroDivisionError: |
| 395 |
exc_info_tuple = sys.exc_info() |
| 396 |
# Register an error by hand (to check the API) |
| 397 |
result.addSubTest(test, subtest, exc_info_tuple) |
| 398 |
# Now trigger a failure |
| 399 |
self.fail("some recognizable failure") |
| 400 |
|
| 401 |
subtest = None |
| 402 |
test = Foo('test_1') |
| 403 |
result = unittest.TestResult() |
| 404 |
|
| 405 |
test.run(result) |
| 406 |
|
| 407 |
self.assertFalse(result.wasSuccessful()) |
| 408 |
self.assertEqual(len(result.errors), 1) |
| 409 |
self.assertEqual(len(result.failures), 1) |
| 410 |
self.assertEqual(result.testsRun, 1) |
| 411 |
self.assertEqual(result.shouldStop, False) |
| 412 |
|
| 413 |
test_case, formatted_exc = result.errors[0] |
| 414 |
self.assertIs(test_case, subtest) |
| 415 |
self.assertIn("ZeroDivisionError", formatted_exc) |
| 416 |
test_case, formatted_exc = result.failures[0] |
| 417 |
self.assertIs(test_case, subtest) |
| 418 |
self.assertIn("some recognizable failure", formatted_exc) |
| 419 |
|
| 420 |
def testStackFrameTrimming(self): |
| 421 |
class Frame(object): |
| 422 |
class tb_frame(object): |
| 423 |
f_globals = {} |
| 424 |
result = unittest.TestResult() |
| 425 |
self.assertFalse(result._is_relevant_tb_level(Frame)) |
| 426 |
|
| 427 |
Frame.tb_frame.f_globals['__unittest'] = True |
| 428 |
self.assertTrue(result._is_relevant_tb_level(Frame)) |
| 429 |
|
| 430 |
def testFailFast(self): |
| 431 |
result = unittest.TestResult() |
| 432 |
result._exc_info_to_string = lambda *_: '' |
| 433 |
result.failfast = True |
| 434 |
result.addError(None, None) |
| 435 |
self.assertTrue(result.shouldStop) |
| 436 |
|
| 437 |
result = unittest.TestResult() |
| 438 |
result._exc_info_to_string = lambda *_: '' |
| 439 |
result.failfast = True |
| 440 |
result.addFailure(None, None) |
| 441 |
self.assertTrue(result.shouldStop) |
| 442 |
|
| 443 |
result = unittest.TestResult() |
| 444 |
result._exc_info_to_string = lambda *_: '' |
| 445 |
result.failfast = True |
| 446 |
result.addUnexpectedSuccess(None) |
| 447 |
self.assertTrue(result.shouldStop) |
| 448 |
|
| 449 |
def testFailFastSetByRunner(self): |
| 450 |
stream = BufferedWriter() |
| 451 |
runner = unittest.TextTestRunner(stream=stream, failfast=True) |
| 452 |
def test(result): |
| 453 |
result.testsRun += 1 |
| 454 |
self.assertTrue(result.failfast) |
| 455 |
result = runner.run(test) |
| 456 |
stream.flush() |
| 457 |
self.assertEndsWith(stream.getvalue(), '\n\nOK\n') |
| 458 |
|
| 459 |
|
| 460 |
@force_not_colorized_test_class |
| 461 |
class Test_TextTestResult(unittest.TestCase): |
| 462 |
maxDiff = None |
| 463 |
|
| 464 |
def testGetDescriptionWithoutDocstring(self): |
| 465 |
result = unittest.TextTestResult(None, True, 1) |
| 466 |
self.assertEqual( |
| 467 |
result.getDescription(self), |
| 468 |
'testGetDescriptionWithoutDocstring (' + __name__ + |
| 469 |
'.Test_TextTestResult.testGetDescriptionWithoutDocstring)') |
| 470 |
|
| 471 |
def testGetSubTestDescriptionWithoutDocstring(self): |
| 472 |
with self.subTest(foo=1, bar=2): |
| 473 |
result = unittest.TextTestResult(None, True, 1) |
| 474 |
self.assertEqual( |
| 475 |
result.getDescription(self._subtest), |
| 476 |
'testGetSubTestDescriptionWithoutDocstring (' + __name__ + |
| 477 |
'.Test_TextTestResult.testGetSubTestDescriptionWithoutDocstring) (foo=1, bar=2)') |
| 478 |
|
| 479 |
with self.subTest('some message'): |
| 480 |
result = unittest.TextTestResult(None, True, 1) |
| 481 |
self.assertEqual( |
| 482 |
result.getDescription(self._subtest), |
| 483 |
'testGetSubTestDescriptionWithoutDocstring (' + __name__ + |
| 484 |
'.Test_TextTestResult.testGetSubTestDescriptionWithoutDocstring) [some message]') |
| 485 |
|
| 486 |
def testGetSubTestDescriptionWithoutDocstringAndParams(self): |
| 487 |
with self.subTest(): |
| 488 |
result = unittest.TextTestResult(None, True, 1) |
| 489 |
self.assertEqual( |
| 490 |
result.getDescription(self._subtest), |
| 491 |
'testGetSubTestDescriptionWithoutDocstringAndParams ' |
| 492 |
'(' + __name__ + '.Test_TextTestResult.testGetSubTestDescriptionWithoutDocstringAndParams) ' |
| 493 |
'(<subtest>)') |
| 494 |
|
| 495 |
def testGetSubTestDescriptionForFalseValues(self): |
| 496 |
expected = 'testGetSubTestDescriptionForFalseValues (%s.Test_TextTestResult.testGetSubTestDescriptionForFalseValues) [%s]' |
| 497 |
result = unittest.TextTestResult(None, True, 1) |
| 498 |
for arg in [0, None, []]: |
| 499 |
with self.subTest(arg): |
| 500 |
self.assertEqual( |
| 501 |
result.getDescription(self._subtest), |
| 502 |
expected % (__name__, arg) |
| 503 |
) |
| 504 |
|
| 505 |
def testGetNestedSubTestDescriptionWithoutDocstring(self): |
| 506 |
with self.subTest(foo=1): |
| 507 |
with self.subTest(baz=2, bar=3): |
| 508 |
result = unittest.TextTestResult(None, True, 1) |
| 509 |
self.assertEqual( |
| 510 |
result.getDescription(self._subtest), |
| 511 |
'testGetNestedSubTestDescriptionWithoutDocstring ' |
| 512 |
'(' + __name__ + '.Test_TextTestResult.testGetNestedSubTestDescriptionWithoutDocstring) ' |
| 513 |
'(baz=2, bar=3, foo=1)') |
| 514 |
|
| 515 |
def testGetDuplicatedNestedSubTestDescriptionWithoutDocstring(self): |
| 516 |
with self.subTest(foo=1, bar=2): |
| 517 |
with self.subTest(baz=3, bar=4): |
| 518 |
result = unittest.TextTestResult(None, True, 1) |
| 519 |
self.assertEqual( |
| 520 |
result.getDescription(self._subtest), |
| 521 |
'testGetDuplicatedNestedSubTestDescriptionWithoutDocstring ' |
| 522 |
'(' + __name__ + '.Test_TextTestResult.testGetDuplicatedNestedSubTestDescriptionWithoutDocstring) (baz=3, bar=4, foo=1)') |
| 523 |
|
| 524 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 525 |
"Docstrings are omitted with -O2 and above") |
| 526 |
def testGetDescriptionWithOneLineDocstring(self): |
| 527 |
"""Tests getDescription() for a method with a docstring.""" |
| 528 |
result = unittest.TextTestResult(None, True, 1) |
| 529 |
self.assertEqual( |
| 530 |
result.getDescription(self), |
| 531 |
('testGetDescriptionWithOneLineDocstring ' |
| 532 |
'(' + __name__ + '.Test_TextTestResult.testGetDescriptionWithOneLineDocstring)\n' |
| 533 |
'Tests getDescription() for a method with a docstring.')) |
| 534 |
|
| 535 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 536 |
"Docstrings are omitted with -O2 and above") |
| 537 |
def testGetSubTestDescriptionWithOneLineDocstring(self): |
| 538 |
"""Tests getDescription() for a method with a docstring.""" |
| 539 |
result = unittest.TextTestResult(None, True, 1) |
| 540 |
with self.subTest(foo=1, bar=2): |
| 541 |
self.assertEqual( |
| 542 |
result.getDescription(self._subtest), |
| 543 |
('testGetSubTestDescriptionWithOneLineDocstring ' |
| 544 |
'(' + __name__ + '.Test_TextTestResult.testGetSubTestDescriptionWithOneLineDocstring) ' |
| 545 |
'(foo=1, bar=2)\n' |
| 546 |
|
| 547 |
'Tests getDescription() for a method with a docstring.')) |
| 548 |
|
| 549 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 550 |
"Docstrings are omitted with -O2 and above") |
| 551 |
def testGetDescriptionWithMultiLineDocstring(self): |
| 552 |
"""Tests getDescription() for a method with a longer docstring. |
| 553 |
The second line of the docstring. |
| 554 |
""" |
| 555 |
result = unittest.TextTestResult(None, True, 1) |
| 556 |
self.assertEqual( |
| 557 |
result.getDescription(self), |
| 558 |
('testGetDescriptionWithMultiLineDocstring ' |
| 559 |
'(' + __name__ + '.Test_TextTestResult.testGetDescriptionWithMultiLineDocstring)\n' |
| 560 |
'Tests getDescription() for a method with a longer ' |
| 561 |
'docstring.')) |
| 562 |
|
| 563 |
@unittest.skipIf(sys.flags.optimize >= 2, |
| 564 |
"Docstrings are omitted with -O2 and above") |
| 565 |
def testGetSubTestDescriptionWithMultiLineDocstring(self): |
| 566 |
"""Tests getDescription() for a method with a longer docstring. |
| 567 |
The second line of the docstring. |
| 568 |
""" |
| 569 |
result = unittest.TextTestResult(None, True, 1) |
| 570 |
with self.subTest(foo=1, bar=2): |
| 571 |
self.assertEqual( |
| 572 |
result.getDescription(self._subtest), |
| 573 |
('testGetSubTestDescriptionWithMultiLineDocstring ' |
| 574 |
'(' + __name__ + '.Test_TextTestResult.testGetSubTestDescriptionWithMultiLineDocstring) ' |
| 575 |
'(foo=1, bar=2)\n' |
| 576 |
'Tests getDescription() for a method with a longer ' |
| 577 |
'docstring.')) |
| 578 |
|
| 579 |
class Test(unittest.TestCase): |
| 580 |
def testSuccess(self): |
| 581 |
pass |
| 582 |
def testSkip(self): |
| 583 |
self.skipTest('skip') |
| 584 |
def testFail(self): |
| 585 |
self.fail('fail') |
| 586 |
def testError(self): |
| 587 |
raise Exception('error') |
| 588 |
@unittest.expectedFailure |
| 589 |
def testExpectedFailure(self): |
| 590 |
self.fail('fail') |
| 591 |
@unittest.expectedFailure |
| 592 |
def testUnexpectedSuccess(self): |
| 593 |
pass |
| 594 |
def testSubTestSuccess(self): |
| 595 |
with self.subTest('one', a=1): |
| 596 |
pass |
| 597 |
with self.subTest('two', b=2): |
| 598 |
pass |
| 599 |
def testSubTestMixed(self): |
| 600 |
with self.subTest('success', a=1): |
| 601 |
pass |
| 602 |
with self.subTest('skip', b=2): |
| 603 |
self.skipTest('skip') |
| 604 |
with self.subTest('fail', c=3): |
| 605 |
self.fail('fail') |
| 606 |
with self.subTest('error', d=4): |
| 607 |
raise Exception('error') |
| 608 |
|
| 609 |
tearDownError = None |
| 610 |
def tearDown(self): |
| 611 |
if self.tearDownError is not None: |
| 612 |
raise self.tearDownError |
| 613 |
|
| 614 |
def _run_test(self, test_name, verbosity, tearDownError=None): |
| 615 |
stream = BufferedWriter() |
| 616 |
stream = unittest.runner._WritelnDecorator(stream) |
| 617 |
result = unittest.TextTestResult(stream, True, verbosity) |
| 618 |
test = self.Test(test_name) |
| 619 |
test.tearDownError = tearDownError |
| 620 |
test.run(result) |
| 621 |
return stream.getvalue() |
| 622 |
|
| 623 |
def testDotsOutput(self): |
| 624 |
self.assertEqual(self._run_test('testSuccess', 1), '.') |
| 625 |
self.assertEqual(self._run_test('testSkip', 1), 's') |
| 626 |
self.assertEqual(self._run_test('testFail', 1), 'F') |
| 627 |
self.assertEqual(self._run_test('testError', 1), 'E') |
| 628 |
self.assertEqual(self._run_test('testExpectedFailure', 1), 'x') |
| 629 |
self.assertEqual(self._run_test('testUnexpectedSuccess', 1), 'u') |
| 630 |
|
| 631 |
def testLongOutput(self): |
| 632 |
classname = f'{__name__}.{self.Test.__qualname__}' |
| 633 |
self.assertEqual(self._run_test('testSuccess', 2), |
| 634 |
f'testSuccess ({classname}.testSuccess) ... ok\n') |
| 635 |
self.assertEqual(self._run_test('testSkip', 2), |
| 636 |
f"testSkip ({classname}.testSkip) ... skipped 'skip'\n") |
| 637 |
self.assertEqual(self._run_test('testFail', 2), |
| 638 |
f'testFail ({classname}.testFail) ... FAIL\n') |
| 639 |
self.assertEqual(self._run_test('testError', 2), |
| 640 |
f'testError ({classname}.testError) ... ERROR\n') |
| 641 |
self.assertEqual(self._run_test('testExpectedFailure', 2), |
| 642 |
f'testExpectedFailure ({classname}.testExpectedFailure) ... expected failure\n') |
| 643 |
self.assertEqual(self._run_test('testUnexpectedSuccess', 2), |
| 644 |
f'testUnexpectedSuccess ({classname}.testUnexpectedSuccess) ... unexpected success\n') |
| 645 |
|
| 646 |
def testDotsOutputSubTestSuccess(self): |
| 647 |
self.assertEqual(self._run_test('testSubTestSuccess', 1), '.') |
| 648 |
|
| 649 |
def testLongOutputSubTestSuccess(self): |
| 650 |
classname = f'{__name__}.{self.Test.__qualname__}' |
| 651 |
self.assertEqual(self._run_test('testSubTestSuccess', 2), |
| 652 |
f'testSubTestSuccess ({classname}.testSubTestSuccess) ... ok\n') |
| 653 |
|
| 654 |
def testDotsOutputSubTestMixed(self): |
| 655 |
self.assertEqual(self._run_test('testSubTestMixed', 1), 'sFE') |
| 656 |
|
| 657 |
def testLongOutputSubTestMixed(self): |
| 658 |
classname = f'{__name__}.{self.Test.__qualname__}' |
| 659 |
self.assertEqual(self._run_test('testSubTestMixed', 2), |
| 660 |
f'testSubTestMixed ({classname}.testSubTestMixed) ... \n' |
| 661 |
f" testSubTestMixed ({classname}.testSubTestMixed) [skip] (b=2) ... skipped 'skip'\n" |
| 662 |
f' testSubTestMixed ({classname}.testSubTestMixed) [fail] (c=3) ... FAIL\n' |
| 663 |
f' testSubTestMixed ({classname}.testSubTestMixed) [error] (d=4) ... ERROR\n') |
| 664 |
|
| 665 |
def testDotsOutputTearDownFail(self): |
| 666 |
out = self._run_test('testSuccess', 1, AssertionError('fail')) |
| 667 |
self.assertEqual(out, 'F') |
| 668 |
out = self._run_test('testError', 1, AssertionError('fail')) |
| 669 |
self.assertEqual(out, 'EF') |
| 670 |
out = self._run_test('testFail', 1, Exception('error')) |
| 671 |
self.assertEqual(out, 'FE') |
| 672 |
out = self._run_test('testSkip', 1, AssertionError('fail')) |
| 673 |
self.assertEqual(out, 'sF') |
| 674 |
|
| 675 |
def testLongOutputTearDownFail(self): |
| 676 |
classname = f'{__name__}.{self.Test.__qualname__}' |
| 677 |
out = self._run_test('testSuccess', 2, AssertionError('fail')) |
| 678 |
self.assertEqual(out, |
| 679 |
f'testSuccess ({classname}.testSuccess) ... FAIL\n') |
| 680 |
out = self._run_test('testError', 2, AssertionError('fail')) |
| 681 |
self.assertEqual(out, |
| 682 |
f'testError ({classname}.testError) ... ERROR\n' |
| 683 |
f'testError ({classname}.testError) ... FAIL\n') |
| 684 |
out = self._run_test('testFail', 2, Exception('error')) |
| 685 |
self.assertEqual(out, |
| 686 |
f'testFail ({classname}.testFail) ... FAIL\n' |
| 687 |
f'testFail ({classname}.testFail) ... ERROR\n') |
| 688 |
out = self._run_test('testSkip', 2, AssertionError('fail')) |
| 689 |
self.assertEqual(out, |
| 690 |
f"testSkip ({classname}.testSkip) ... skipped 'skip'\n" |
| 691 |
f'testSkip ({classname}.testSkip) ... FAIL\n') |
| 692 |
|
| 693 |
|
| 694 |
classDict = dict(unittest.TestResult.__dict__) |
| 695 |
for m in ('addSkip', 'addExpectedFailure', 'addUnexpectedSuccess', |
| 696 |
'__init__'): |
| 697 |
del classDict[m] |
| 698 |
|
| 699 |
def __init__(self, stream=None, descriptions=None, verbosity=None): |
| 700 |
self.failures = [] |
| 701 |
self.errors = [] |
| 702 |
self.testsRun = 0 |
| 703 |
self.shouldStop = False |
| 704 |
self.buffer = False |
| 705 |
self.tb_locals = False |
| 706 |
|
| 707 |
classDict['__init__'] = __init__ |
| 708 |
OldResult = type('OldResult', (object,), classDict) |
| 709 |
|
| 710 |
class Test_OldTestResult(unittest.TestCase): |
| 711 |
|
| 712 |
def assertOldResultWarning(self, test, failures): |
| 713 |
with warnings_helper.check_warnings( |
| 714 |
("TestResult has no add.+ method,", RuntimeWarning)): |
| 715 |
result = OldResult() |
| 716 |
test.run(result) |
| 717 |
self.assertEqual(len(result.failures), failures) |
| 718 |
|
| 719 |
def testOldTestResult(self): |
| 720 |
class Test(unittest.TestCase): |
| 721 |
def testSkip(self): |
| 722 |
self.skipTest('foobar') |
| 723 |
@unittest.expectedFailure |
| 724 |
def testExpectedFail(self): |
| 725 |
raise TypeError |
| 726 |
@unittest.expectedFailure |
| 727 |
def testUnexpectedSuccess(self): |
| 728 |
pass |
| 729 |
|
| 730 |
for test_name, should_pass in (('testSkip', True), |
| 731 |
('testExpectedFail', True), |
| 732 |
('testUnexpectedSuccess', False)): |
| 733 |
test = Test(test_name) |
| 734 |
self.assertOldResultWarning(test, int(not should_pass)) |
| 735 |
|
| 736 |
def testOldTestTesultSetup(self): |
| 737 |
class Test(unittest.TestCase): |
| 738 |
def setUp(self): |
| 739 |
self.skipTest('no reason') |
| 740 |
def testFoo(self): |
| 741 |
pass |
| 742 |
self.assertOldResultWarning(Test('testFoo'), 0) |
| 743 |
|
| 744 |
def testOldTestResultClass(self): |
| 745 |
@unittest.skip('no reason') |
| 746 |
class Test(unittest.TestCase): |
| 747 |
def testFoo(self): |
| 748 |
pass |
| 749 |
self.assertOldResultWarning(Test('testFoo'), 0) |
| 750 |
|
| 751 |
def testOldResultWithRunner(self): |
| 752 |
class Test(unittest.TestCase): |
| 753 |
def testFoo(self): |
| 754 |
pass |
| 755 |
runner = unittest.TextTestRunner(resultclass=OldResult, |
| 756 |
stream=io.StringIO()) |
| 757 |
# This will raise an exception if TextTestRunner can't handle old |
| 758 |
# test result objects |
| 759 |
runner.run(Test('testFoo')) |
| 760 |
|
| 761 |
|
| 762 |
@force_not_colorized_test_class |
| 763 |
class TestOutputBuffering(unittest.TestCase): |
| 764 |
|
| 765 |
def setUp(self): |
| 766 |
self._real_out = sys.stdout |
| 767 |
self._real_err = sys.stderr |
| 768 |
|
| 769 |
def tearDown(self): |
| 770 |
sys.stdout = self._real_out |
| 771 |
sys.stderr = self._real_err |
| 772 |
|
| 773 |
def testBufferOutputOff(self): |
| 774 |
real_out = self._real_out |
| 775 |
real_err = self._real_err |
| 776 |
|
| 777 |
result = unittest.TestResult() |
| 778 |
self.assertFalse(result.buffer) |
| 779 |
|
| 780 |
self.assertIs(real_out, sys.stdout) |
| 781 |
self.assertIs(real_err, sys.stderr) |
| 782 |
|
| 783 |
result.startTest(self) |
| 784 |
|
| 785 |
self.assertIs(real_out, sys.stdout) |
| 786 |
self.assertIs(real_err, sys.stderr) |
| 787 |
|
| 788 |
def testBufferOutputStartTestAddSuccess(self): |
| 789 |
real_out = self._real_out |
| 790 |
real_err = self._real_err |
| 791 |
|
| 792 |
result = unittest.TestResult() |
| 793 |
self.assertFalse(result.buffer) |
| 794 |
|
| 795 |
result.buffer = True |
| 796 |
|
| 797 |
self.assertIs(real_out, sys.stdout) |
| 798 |
self.assertIs(real_err, sys.stderr) |
| 799 |
|
| 800 |
result.startTest(self) |
| 801 |
|
| 802 |
self.assertIsNot(real_out, sys.stdout) |
| 803 |
self.assertIsNot(real_err, sys.stderr) |
| 804 |
self.assertIsInstance(sys.stdout, io.StringIO) |
| 805 |
self.assertIsInstance(sys.stderr, io.StringIO) |
| 806 |
self.assertIsNot(sys.stdout, sys.stderr) |
| 807 |
|
| 808 |
out_stream = sys.stdout |
| 809 |
err_stream = sys.stderr |
| 810 |
|
| 811 |
result._original_stdout = io.StringIO() |
| 812 |
result._original_stderr = io.StringIO() |
| 813 |
|
| 814 |
print('foo') |
| 815 |
print('bar', file=sys.stderr) |
| 816 |
|
| 817 |
self.assertEqual(out_stream.getvalue(), 'foo\n') |
| 818 |
self.assertEqual(err_stream.getvalue(), 'bar\n') |
| 819 |
|
| 820 |
self.assertEqual(result._original_stdout.getvalue(), '') |
| 821 |
self.assertEqual(result._original_stderr.getvalue(), '') |
| 822 |
|
| 823 |
result.addSuccess(self) |
| 824 |
result.stopTest(self) |
| 825 |
|
| 826 |
self.assertIs(sys.stdout, result._original_stdout) |
| 827 |
self.assertIs(sys.stderr, result._original_stderr) |
| 828 |
|
| 829 |
self.assertEqual(result._original_stdout.getvalue(), '') |
| 830 |
self.assertEqual(result._original_stderr.getvalue(), '') |
| 831 |
|
| 832 |
self.assertEqual(out_stream.getvalue(), '') |
| 833 |
self.assertEqual(err_stream.getvalue(), '') |
| 834 |
|
| 835 |
|
| 836 |
def getStartedResult(self): |
| 837 |
result = unittest.TestResult() |
| 838 |
result.buffer = True |
| 839 |
result.startTest(self) |
| 840 |
return result |
| 841 |
|
| 842 |
def testBufferOutputAddErrorOrFailure(self): |
| 843 |
unittest.result.traceback = MockTraceback |
| 844 |
self.addCleanup(restore_traceback) |
| 845 |
|
| 846 |
for message_attr, add_attr, include_error in [ |
| 847 |
('errors', 'addError', True), |
| 848 |
('failures', 'addFailure', False), |
| 849 |
('errors', 'addError', True), |
| 850 |
('failures', 'addFailure', False) |
| 851 |
]: |
| 852 |
result = self.getStartedResult() |
| 853 |
buffered_out = sys.stdout |
| 854 |
buffered_err = sys.stderr |
| 855 |
result._original_stdout = io.StringIO() |
| 856 |
result._original_stderr = io.StringIO() |
| 857 |
|
| 858 |
print('foo', file=sys.stdout) |
| 859 |
if include_error: |
| 860 |
print('bar', file=sys.stderr) |
| 861 |
|
| 862 |
|
| 863 |
addFunction = getattr(result, add_attr) |
| 864 |
addFunction(self, (None, None, None)) |
| 865 |
result.stopTest(self) |
| 866 |
|
| 867 |
result_list = getattr(result, message_attr) |
| 868 |
self.assertEqual(len(result_list), 1) |
| 869 |
|
| 870 |
test, message = result_list[0] |
| 871 |
expectedOutMessage = textwrap.dedent(""" |
| 872 |
Stdout: |
| 873 |
foo |
| 874 |
""") |
| 875 |
expectedErrMessage = '' |
| 876 |
if include_error: |
| 877 |
expectedErrMessage = textwrap.dedent(""" |
| 878 |
Stderr: |
| 879 |
bar |
| 880 |
""") |
| 881 |
|
| 882 |
expectedFullMessage = 'A traceback%s%s' % (expectedOutMessage, expectedErrMessage) |
| 883 |
|
| 884 |
self.assertIs(test, self) |
| 885 |
self.assertEqual(result._original_stdout.getvalue(), expectedOutMessage) |
| 886 |
self.assertEqual(result._original_stderr.getvalue(), expectedErrMessage) |
| 887 |
self.assertMultiLineEqual(message, expectedFullMessage) |
| 888 |
|
| 889 |
def testBufferSetUp(self): |
| 890 |
with captured_stdout() as stdout: |
| 891 |
result = unittest.TestResult() |
| 892 |
result.buffer = True |
| 893 |
|
| 894 |
class Foo(unittest.TestCase): |
| 895 |
def setUp(self): |
| 896 |
print('set up') |
| 897 |
1/0 |
| 898 |
def test_foo(self): |
| 899 |
pass |
| 900 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 901 |
suite(result) |
| 902 |
expected_out = '\nStdout:\nset up\n' |
| 903 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 904 |
self.assertEqual(len(result.errors), 1) |
| 905 |
description = f'test_foo ({strclass(Foo)}.test_foo)' |
| 906 |
test_case, formatted_exc = result.errors[0] |
| 907 |
self.assertEqual(str(test_case), description) |
| 908 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 909 |
self.assertIn(expected_out, formatted_exc) |
| 910 |
|
| 911 |
def testBufferTearDown(self): |
| 912 |
with captured_stdout() as stdout: |
| 913 |
result = unittest.TestResult() |
| 914 |
result.buffer = True |
| 915 |
|
| 916 |
class Foo(unittest.TestCase): |
| 917 |
def tearDown(self): |
| 918 |
print('tear down') |
| 919 |
1/0 |
| 920 |
def test_foo(self): |
| 921 |
pass |
| 922 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 923 |
suite(result) |
| 924 |
expected_out = '\nStdout:\ntear down\n' |
| 925 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 926 |
self.assertEqual(len(result.errors), 1) |
| 927 |
description = f'test_foo ({strclass(Foo)}.test_foo)' |
| 928 |
test_case, formatted_exc = result.errors[0] |
| 929 |
self.assertEqual(str(test_case), description) |
| 930 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 931 |
self.assertIn(expected_out, formatted_exc) |
| 932 |
|
| 933 |
def testBufferDoCleanups(self): |
| 934 |
with captured_stdout() as stdout: |
| 935 |
result = unittest.TestResult() |
| 936 |
result.buffer = True |
| 937 |
|
| 938 |
class Foo(unittest.TestCase): |
| 939 |
def setUp(self): |
| 940 |
print('set up') |
| 941 |
self.addCleanup(bad_cleanup1) |
| 942 |
self.addCleanup(bad_cleanup2) |
| 943 |
def test_foo(self): |
| 944 |
pass |
| 945 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 946 |
suite(result) |
| 947 |
expected_out = '\nStdout:\nset up\ndo cleanup2\ndo cleanup1\n' |
| 948 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 949 |
self.assertEqual(len(result.errors), 2) |
| 950 |
description = f'test_foo ({strclass(Foo)}.test_foo)' |
| 951 |
test_case, formatted_exc = result.errors[0] |
| 952 |
self.assertEqual(str(test_case), description) |
| 953 |
self.assertIn('ValueError: bad cleanup2', formatted_exc) |
| 954 |
self.assertNotIn('TypeError', formatted_exc) |
| 955 |
self.assertIn('\nStdout:\nset up\ndo cleanup2\n', formatted_exc) |
| 956 |
self.assertNotIn('\ndo cleanup1\n', formatted_exc) |
| 957 |
test_case, formatted_exc = result.errors[1] |
| 958 |
self.assertEqual(str(test_case), description) |
| 959 |
self.assertIn('TypeError: bad cleanup1', formatted_exc) |
| 960 |
self.assertNotIn('ValueError', formatted_exc) |
| 961 |
self.assertIn(expected_out, formatted_exc) |
| 962 |
|
| 963 |
def testBufferSetUp_DoCleanups(self): |
| 964 |
with captured_stdout() as stdout: |
| 965 |
result = unittest.TestResult() |
| 966 |
result.buffer = True |
| 967 |
|
| 968 |
class Foo(unittest.TestCase): |
| 969 |
def setUp(self): |
| 970 |
print('set up') |
| 971 |
self.addCleanup(bad_cleanup1) |
| 972 |
self.addCleanup(bad_cleanup2) |
| 973 |
1/0 |
| 974 |
def test_foo(self): |
| 975 |
pass |
| 976 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 977 |
suite(result) |
| 978 |
expected_out = '\nStdout:\nset up\ndo cleanup2\ndo cleanup1\n' |
| 979 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 980 |
self.assertEqual(len(result.errors), 3) |
| 981 |
description = f'test_foo ({strclass(Foo)}.test_foo)' |
| 982 |
test_case, formatted_exc = result.errors[0] |
| 983 |
self.assertEqual(str(test_case), description) |
| 984 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 985 |
self.assertNotIn('ValueError', formatted_exc) |
| 986 |
self.assertNotIn('TypeError', formatted_exc) |
| 987 |
self.assertIn('\nStdout:\nset up\n', formatted_exc) |
| 988 |
self.assertNotIn('\ndo cleanup2\n', formatted_exc) |
| 989 |
self.assertNotIn('\ndo cleanup1\n', formatted_exc) |
| 990 |
test_case, formatted_exc = result.errors[1] |
| 991 |
self.assertEqual(str(test_case), description) |
| 992 |
self.assertIn('ValueError: bad cleanup2', formatted_exc) |
| 993 |
self.assertNotIn('ZeroDivisionError', formatted_exc) |
| 994 |
self.assertNotIn('TypeError', formatted_exc) |
| 995 |
self.assertIn('\nStdout:\nset up\ndo cleanup2\n', formatted_exc) |
| 996 |
self.assertNotIn('\ndo cleanup1\n', formatted_exc) |
| 997 |
test_case, formatted_exc = result.errors[2] |
| 998 |
self.assertEqual(str(test_case), description) |
| 999 |
self.assertIn('TypeError: bad cleanup1', formatted_exc) |
| 1000 |
self.assertNotIn('ZeroDivisionError', formatted_exc) |
| 1001 |
self.assertNotIn('ValueError', formatted_exc) |
| 1002 |
self.assertIn(expected_out, formatted_exc) |
| 1003 |
|
| 1004 |
def testBufferTearDown_DoCleanups(self): |
| 1005 |
with captured_stdout() as stdout: |
| 1006 |
result = unittest.TestResult() |
| 1007 |
result.buffer = True |
| 1008 |
|
| 1009 |
class Foo(unittest.TestCase): |
| 1010 |
def setUp(self): |
| 1011 |
print('set up') |
| 1012 |
self.addCleanup(bad_cleanup1) |
| 1013 |
self.addCleanup(bad_cleanup2) |
| 1014 |
def tearDown(self): |
| 1015 |
print('tear down') |
| 1016 |
1/0 |
| 1017 |
def test_foo(self): |
| 1018 |
pass |
| 1019 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 1020 |
suite(result) |
| 1021 |
expected_out = '\nStdout:\nset up\ntear down\ndo cleanup2\ndo cleanup1\n' |
| 1022 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 1023 |
self.assertEqual(len(result.errors), 3) |
| 1024 |
description = f'test_foo ({strclass(Foo)}.test_foo)' |
| 1025 |
test_case, formatted_exc = result.errors[0] |
| 1026 |
self.assertEqual(str(test_case), description) |
| 1027 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 1028 |
self.assertNotIn('ValueError', formatted_exc) |
| 1029 |
self.assertNotIn('TypeError', formatted_exc) |
| 1030 |
self.assertIn('\nStdout:\nset up\ntear down\n', formatted_exc) |
| 1031 |
self.assertNotIn('\ndo cleanup2\n', formatted_exc) |
| 1032 |
self.assertNotIn('\ndo cleanup1\n', formatted_exc) |
| 1033 |
test_case, formatted_exc = result.errors[1] |
| 1034 |
self.assertEqual(str(test_case), description) |
| 1035 |
self.assertIn('ValueError: bad cleanup2', formatted_exc) |
| 1036 |
self.assertNotIn('ZeroDivisionError', formatted_exc) |
| 1037 |
self.assertNotIn('TypeError', formatted_exc) |
| 1038 |
self.assertIn('\nStdout:\nset up\ntear down\ndo cleanup2\n', formatted_exc) |
| 1039 |
self.assertNotIn('\ndo cleanup1\n', formatted_exc) |
| 1040 |
test_case, formatted_exc = result.errors[2] |
| 1041 |
self.assertEqual(str(test_case), description) |
| 1042 |
self.assertIn('TypeError: bad cleanup1', formatted_exc) |
| 1043 |
self.assertNotIn('ZeroDivisionError', formatted_exc) |
| 1044 |
self.assertNotIn('ValueError', formatted_exc) |
| 1045 |
self.assertIn(expected_out, formatted_exc) |
| 1046 |
|
| 1047 |
def testBufferSetupClass(self): |
| 1048 |
with captured_stdout() as stdout: |
| 1049 |
result = unittest.TestResult() |
| 1050 |
result.buffer = True |
| 1051 |
|
| 1052 |
class Foo(unittest.TestCase): |
| 1053 |
@classmethod |
| 1054 |
def setUpClass(cls): |
| 1055 |
print('set up class') |
| 1056 |
1/0 |
| 1057 |
def test_foo(self): |
| 1058 |
pass |
| 1059 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 1060 |
suite(result) |
| 1061 |
expected_out = '\nStdout:\nset up class\n' |
| 1062 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 1063 |
self.assertEqual(len(result.errors), 1) |
| 1064 |
description = f'setUpClass ({strclass(Foo)})' |
| 1065 |
test_case, formatted_exc = result.errors[0] |
| 1066 |
self.assertEqual(test_case.description, description) |
| 1067 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 1068 |
self.assertIn(expected_out, formatted_exc) |
| 1069 |
|
| 1070 |
def testBufferTearDownClass(self): |
| 1071 |
with captured_stdout() as stdout: |
| 1072 |
result = unittest.TestResult() |
| 1073 |
result.buffer = True |
| 1074 |
|
| 1075 |
class Foo(unittest.TestCase): |
| 1076 |
@classmethod |
| 1077 |
def tearDownClass(cls): |
| 1078 |
print('tear down class') |
| 1079 |
1/0 |
| 1080 |
def test_foo(self): |
| 1081 |
pass |
| 1082 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 1083 |
suite(result) |
| 1084 |
expected_out = '\nStdout:\ntear down class\n' |
| 1085 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 1086 |
self.assertEqual(len(result.errors), 1) |
| 1087 |
description = f'tearDownClass ({strclass(Foo)})' |
| 1088 |
test_case, formatted_exc = result.errors[0] |
| 1089 |
self.assertEqual(test_case.description, description) |
| 1090 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 1091 |
self.assertIn(expected_out, formatted_exc) |
| 1092 |
|
| 1093 |
def testBufferDoClassCleanups(self): |
| 1094 |
with captured_stdout() as stdout: |
| 1095 |
result = unittest.TestResult() |
| 1096 |
result.buffer = True |
| 1097 |
|
| 1098 |
class Foo(unittest.TestCase): |
| 1099 |
@classmethod |
| 1100 |
def setUpClass(cls): |
| 1101 |
print('set up class') |
| 1102 |
cls.addClassCleanup(bad_cleanup1) |
| 1103 |
cls.addClassCleanup(bad_cleanup2) |
| 1104 |
@classmethod |
| 1105 |
def tearDownClass(cls): |
| 1106 |
print('tear down class') |
| 1107 |
def test_foo(self): |
| 1108 |
pass |
| 1109 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 1110 |
suite(result) |
| 1111 |
expected_out = '\nStdout:\ntear down class\ndo cleanup2\ndo cleanup1\n' |
| 1112 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 1113 |
self.assertEqual(len(result.errors), 2) |
| 1114 |
description = f'tearDownClass ({strclass(Foo)})' |
| 1115 |
test_case, formatted_exc = result.errors[0] |
| 1116 |
self.assertEqual(test_case.description, description) |
| 1117 |
self.assertIn('ValueError: bad cleanup2', formatted_exc) |
| 1118 |
self.assertNotIn('TypeError', formatted_exc) |
| 1119 |
self.assertIn(expected_out, formatted_exc) |
| 1120 |
test_case, formatted_exc = result.errors[1] |
| 1121 |
self.assertEqual(test_case.description, description) |
| 1122 |
self.assertIn('TypeError: bad cleanup1', formatted_exc) |
| 1123 |
self.assertNotIn('ValueError', formatted_exc) |
| 1124 |
self.assertIn(expected_out, formatted_exc) |
| 1125 |
|
| 1126 |
def testBufferSetupClass_DoClassCleanups(self): |
| 1127 |
with captured_stdout() as stdout: |
| 1128 |
result = unittest.TestResult() |
| 1129 |
result.buffer = True |
| 1130 |
|
| 1131 |
class Foo(unittest.TestCase): |
| 1132 |
@classmethod |
| 1133 |
def setUpClass(cls): |
| 1134 |
print('set up class') |
| 1135 |
cls.addClassCleanup(bad_cleanup1) |
| 1136 |
cls.addClassCleanup(bad_cleanup2) |
| 1137 |
1/0 |
| 1138 |
def test_foo(self): |
| 1139 |
pass |
| 1140 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 1141 |
suite(result) |
| 1142 |
expected_out = '\nStdout:\nset up class\ndo cleanup2\ndo cleanup1\n' |
| 1143 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 1144 |
self.assertEqual(len(result.errors), 3) |
| 1145 |
description = f'setUpClass ({strclass(Foo)})' |
| 1146 |
test_case, formatted_exc = result.errors[0] |
| 1147 |
self.assertEqual(test_case.description, description) |
| 1148 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 1149 |
self.assertNotIn('ValueError', formatted_exc) |
| 1150 |
self.assertNotIn('TypeError', formatted_exc) |
| 1151 |
self.assertIn('\nStdout:\nset up class\n', formatted_exc) |
| 1152 |
test_case, formatted_exc = result.errors[1] |
| 1153 |
self.assertEqual(test_case.description, description) |
| 1154 |
self.assertIn('ValueError: bad cleanup2', formatted_exc) |
| 1155 |
self.assertNotIn('ZeroDivisionError', formatted_exc) |
| 1156 |
self.assertNotIn('TypeError', formatted_exc) |
| 1157 |
self.assertIn(expected_out, formatted_exc) |
| 1158 |
test_case, formatted_exc = result.errors[2] |
| 1159 |
self.assertEqual(test_case.description, description) |
| 1160 |
self.assertIn('TypeError: bad cleanup1', formatted_exc) |
| 1161 |
self.assertNotIn('ZeroDivisionError', formatted_exc) |
| 1162 |
self.assertNotIn('ValueError', formatted_exc) |
| 1163 |
self.assertIn(expected_out, formatted_exc) |
| 1164 |
|
| 1165 |
def testBufferTearDownClass_DoClassCleanups(self): |
| 1166 |
with captured_stdout() as stdout: |
| 1167 |
result = unittest.TestResult() |
| 1168 |
result.buffer = True |
| 1169 |
|
| 1170 |
class Foo(unittest.TestCase): |
| 1171 |
@classmethod |
| 1172 |
def setUpClass(cls): |
| 1173 |
print('set up class') |
| 1174 |
cls.addClassCleanup(bad_cleanup1) |
| 1175 |
cls.addClassCleanup(bad_cleanup2) |
| 1176 |
@classmethod |
| 1177 |
def tearDownClass(cls): |
| 1178 |
print('tear down class') |
| 1179 |
1/0 |
| 1180 |
def test_foo(self): |
| 1181 |
pass |
| 1182 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 1183 |
suite(result) |
| 1184 |
expected_out = '\nStdout:\ntear down class\ndo cleanup2\ndo cleanup1\n' |
| 1185 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 1186 |
self.assertEqual(len(result.errors), 3) |
| 1187 |
description = f'tearDownClass ({strclass(Foo)})' |
| 1188 |
test_case, formatted_exc = result.errors[0] |
| 1189 |
self.assertEqual(test_case.description, description) |
| 1190 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 1191 |
self.assertNotIn('ValueError', formatted_exc) |
| 1192 |
self.assertNotIn('TypeError', formatted_exc) |
| 1193 |
self.assertIn('\nStdout:\ntear down class\n', formatted_exc) |
| 1194 |
test_case, formatted_exc = result.errors[1] |
| 1195 |
self.assertEqual(test_case.description, description) |
| 1196 |
self.assertIn('ValueError: bad cleanup2', formatted_exc) |
| 1197 |
self.assertNotIn('ZeroDivisionError', formatted_exc) |
| 1198 |
self.assertNotIn('TypeError', formatted_exc) |
| 1199 |
self.assertIn(expected_out, formatted_exc) |
| 1200 |
test_case, formatted_exc = result.errors[2] |
| 1201 |
self.assertEqual(test_case.description, description) |
| 1202 |
self.assertIn('TypeError: bad cleanup1', formatted_exc) |
| 1203 |
self.assertNotIn('ZeroDivisionError', formatted_exc) |
| 1204 |
self.assertNotIn('ValueError', formatted_exc) |
| 1205 |
self.assertIn(expected_out, formatted_exc) |
| 1206 |
|
| 1207 |
def testBufferSetUpModule(self): |
| 1208 |
with captured_stdout() as stdout: |
| 1209 |
result = unittest.TestResult() |
| 1210 |
result.buffer = True |
| 1211 |
|
| 1212 |
class Foo(unittest.TestCase): |
| 1213 |
def test_foo(self): |
| 1214 |
pass |
| 1215 |
class Module(object): |
| 1216 |
@staticmethod |
| 1217 |
def setUpModule(): |
| 1218 |
print('set up module') |
| 1219 |
1/0 |
| 1220 |
|
| 1221 |
Foo.__module__ = 'Module' |
| 1222 |
sys.modules['Module'] = Module |
| 1223 |
self.addCleanup(sys.modules.pop, 'Module') |
| 1224 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 1225 |
suite(result) |
| 1226 |
expected_out = '\nStdout:\nset up module\n' |
| 1227 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 1228 |
self.assertEqual(len(result.errors), 1) |
| 1229 |
description = 'setUpModule (Module)' |
| 1230 |
test_case, formatted_exc = result.errors[0] |
| 1231 |
self.assertEqual(test_case.description, description) |
| 1232 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 1233 |
self.assertIn(expected_out, formatted_exc) |
| 1234 |
|
| 1235 |
def testBufferTearDownModule(self): |
| 1236 |
with captured_stdout() as stdout: |
| 1237 |
result = unittest.TestResult() |
| 1238 |
result.buffer = True |
| 1239 |
|
| 1240 |
class Foo(unittest.TestCase): |
| 1241 |
def test_foo(self): |
| 1242 |
pass |
| 1243 |
class Module(object): |
| 1244 |
@staticmethod |
| 1245 |
def tearDownModule(): |
| 1246 |
print('tear down module') |
| 1247 |
1/0 |
| 1248 |
|
| 1249 |
Foo.__module__ = 'Module' |
| 1250 |
sys.modules['Module'] = Module |
| 1251 |
self.addCleanup(sys.modules.pop, 'Module') |
| 1252 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 1253 |
suite(result) |
| 1254 |
expected_out = '\nStdout:\ntear down module\n' |
| 1255 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 1256 |
self.assertEqual(len(result.errors), 1) |
| 1257 |
description = 'tearDownModule (Module)' |
| 1258 |
test_case, formatted_exc = result.errors[0] |
| 1259 |
self.assertEqual(test_case.description, description) |
| 1260 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 1261 |
self.assertIn(expected_out, formatted_exc) |
| 1262 |
|
| 1263 |
def testBufferDoModuleCleanups(self): |
| 1264 |
with captured_stdout() as stdout: |
| 1265 |
result = unittest.TestResult() |
| 1266 |
result.buffer = True |
| 1267 |
|
| 1268 |
class Foo(unittest.TestCase): |
| 1269 |
def test_foo(self): |
| 1270 |
pass |
| 1271 |
class Module(object): |
| 1272 |
@staticmethod |
| 1273 |
def setUpModule(): |
| 1274 |
print('set up module') |
| 1275 |
unittest.addModuleCleanup(bad_cleanup1) |
| 1276 |
unittest.addModuleCleanup(bad_cleanup2) |
| 1277 |
|
| 1278 |
Foo.__module__ = 'Module' |
| 1279 |
sys.modules['Module'] = Module |
| 1280 |
self.addCleanup(sys.modules.pop, 'Module') |
| 1281 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 1282 |
suite(result) |
| 1283 |
expected_out = '\nStdout:\ndo cleanup2\ndo cleanup1\n' |
| 1284 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 1285 |
self.assertEqual(len(result.errors), 1) |
| 1286 |
description = 'tearDownModule (Module)' |
| 1287 |
test_case, formatted_exc = result.errors[0] |
| 1288 |
self.assertEqual(test_case.description, description) |
| 1289 |
self.assertIn('ValueError: bad cleanup2', formatted_exc) |
| 1290 |
self.assertNotIn('TypeError', formatted_exc) |
| 1291 |
self.assertIn(expected_out, formatted_exc) |
| 1292 |
|
| 1293 |
def testBufferSetUpModule_DoModuleCleanups(self): |
| 1294 |
with captured_stdout() as stdout: |
| 1295 |
result = unittest.TestResult() |
| 1296 |
result.buffer = True |
| 1297 |
|
| 1298 |
class Foo(unittest.TestCase): |
| 1299 |
def test_foo(self): |
| 1300 |
pass |
| 1301 |
class Module(object): |
| 1302 |
@staticmethod |
| 1303 |
def setUpModule(): |
| 1304 |
print('set up module') |
| 1305 |
unittest.addModuleCleanup(bad_cleanup1) |
| 1306 |
unittest.addModuleCleanup(bad_cleanup2) |
| 1307 |
1/0 |
| 1308 |
|
| 1309 |
Foo.__module__ = 'Module' |
| 1310 |
sys.modules['Module'] = Module |
| 1311 |
self.addCleanup(sys.modules.pop, 'Module') |
| 1312 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 1313 |
suite(result) |
| 1314 |
expected_out = '\nStdout:\nset up module\ndo cleanup2\ndo cleanup1\n' |
| 1315 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 1316 |
self.assertEqual(len(result.errors), 2) |
| 1317 |
description = 'setUpModule (Module)' |
| 1318 |
test_case, formatted_exc = result.errors[0] |
| 1319 |
self.assertEqual(test_case.description, description) |
| 1320 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 1321 |
self.assertNotIn('ValueError', formatted_exc) |
| 1322 |
self.assertNotIn('TypeError', formatted_exc) |
| 1323 |
self.assertIn('\nStdout:\nset up module\n', formatted_exc) |
| 1324 |
test_case, formatted_exc = result.errors[1] |
| 1325 |
self.assertIn(expected_out, formatted_exc) |
| 1326 |
self.assertEqual(test_case.description, description) |
| 1327 |
self.assertIn('ValueError: bad cleanup2', formatted_exc) |
| 1328 |
self.assertNotIn('ZeroDivisionError', formatted_exc) |
| 1329 |
self.assertNotIn('TypeError', formatted_exc) |
| 1330 |
self.assertIn(expected_out, formatted_exc) |
| 1331 |
|
| 1332 |
def testBufferTearDownModule_DoModuleCleanups(self): |
| 1333 |
with captured_stdout() as stdout: |
| 1334 |
result = unittest.TestResult() |
| 1335 |
result.buffer = True |
| 1336 |
|
| 1337 |
class Foo(unittest.TestCase): |
| 1338 |
def test_foo(self): |
| 1339 |
pass |
| 1340 |
class Module(object): |
| 1341 |
@staticmethod |
| 1342 |
def setUpModule(): |
| 1343 |
print('set up module') |
| 1344 |
unittest.addModuleCleanup(bad_cleanup1) |
| 1345 |
unittest.addModuleCleanup(bad_cleanup2) |
| 1346 |
@staticmethod |
| 1347 |
def tearDownModule(): |
| 1348 |
print('tear down module') |
| 1349 |
1/0 |
| 1350 |
|
| 1351 |
Foo.__module__ = 'Module' |
| 1352 |
sys.modules['Module'] = Module |
| 1353 |
self.addCleanup(sys.modules.pop, 'Module') |
| 1354 |
suite = unittest.TestSuite([Foo('test_foo')]) |
| 1355 |
suite(result) |
| 1356 |
expected_out = '\nStdout:\ntear down module\ndo cleanup2\ndo cleanup1\n' |
| 1357 |
self.assertEqual(stdout.getvalue(), expected_out) |
| 1358 |
self.assertEqual(len(result.errors), 2) |
| 1359 |
description = 'tearDownModule (Module)' |
| 1360 |
test_case, formatted_exc = result.errors[0] |
| 1361 |
self.assertEqual(test_case.description, description) |
| 1362 |
self.assertIn('ZeroDivisionError: division by zero', formatted_exc) |
| 1363 |
self.assertNotIn('ValueError', formatted_exc) |
| 1364 |
self.assertNotIn('TypeError', formatted_exc) |
| 1365 |
self.assertIn('\nStdout:\ntear down module\n', formatted_exc) |
| 1366 |
test_case, formatted_exc = result.errors[1] |
| 1367 |
self.assertEqual(test_case.description, description) |
| 1368 |
self.assertIn('ValueError: bad cleanup2', formatted_exc) |
| 1369 |
self.assertNotIn('ZeroDivisionError', formatted_exc) |
| 1370 |
self.assertNotIn('TypeError', formatted_exc) |
| 1371 |
self.assertIn(expected_out, formatted_exc) |
| 1372 |
|
| 1373 |
|
| 1374 |
if __name__ == '__main__': |
| 1375 |
unittest.main() |