Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 1 | import difflib |
| 2 | import pprint |
Michael Foord | 8ca6d98 | 2010-11-20 15:34:26 +0000 | [diff] [blame] | 3 | import pickle |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 4 | import re |
| 5 | import sys |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 6 | import warnings |
Benjamin Peterson | b6ffa79 | 2011-07-14 12:48:25 -0500 | [diff] [blame] | 7 | import weakref |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 8 | import inspect |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 9 | |
| 10 | from copy import deepcopy |
| 11 | from test import support |
| 12 | |
| 13 | import unittest |
| 14 | |
| 15 | from .support import ( |
Antoine Pitrou | c9b3ef2 | 2013-03-20 20:16:47 +0100 | [diff] [blame] | 16 | TestEquality, TestHashing, LoggingResult, LegacyLoggingResult, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 17 | ResultWithNoStartTestRunStopTestRun |
| 18 | ) |
| 19 | |
| 20 | |
| 21 | class Test(object): |
| 22 | "Keep these TestCase classes out of the main namespace" |
| 23 | |
| 24 | class Foo(unittest.TestCase): |
| 25 | def runTest(self): pass |
| 26 | def test1(self): pass |
| 27 | |
| 28 | class Bar(Foo): |
| 29 | def test2(self): pass |
| 30 | |
| 31 | class LoggingTestCase(unittest.TestCase): |
| 32 | """A test case which logs its calls.""" |
| 33 | |
| 34 | def __init__(self, events): |
| 35 | super(Test.LoggingTestCase, self).__init__('test') |
| 36 | self.events = events |
| 37 | |
| 38 | def setUp(self): |
| 39 | self.events.append('setUp') |
| 40 | |
| 41 | def test(self): |
| 42 | self.events.append('test') |
| 43 | |
| 44 | def tearDown(self): |
| 45 | self.events.append('tearDown') |
| 46 | |
| 47 | |
| 48 | class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): |
| 49 | |
| 50 | ### Set up attributes used by inherited tests |
| 51 | ################################################################ |
| 52 | |
| 53 | # Used by TestHashing.test_hash and TestEquality.test_eq |
| 54 | eq_pairs = [(Test.Foo('test1'), Test.Foo('test1'))] |
| 55 | |
| 56 | # Used by TestEquality.test_ne |
| 57 | ne_pairs = [(Test.Foo('test1'), Test.Foo('runTest')), |
| 58 | (Test.Foo('test1'), Test.Bar('test1')), |
| 59 | (Test.Foo('test1'), Test.Bar('test2'))] |
| 60 | |
| 61 | ################################################################ |
| 62 | ### /Set up attributes used by inherited tests |
| 63 | |
| 64 | |
| 65 | # "class TestCase([methodName])" |
| 66 | # ... |
| 67 | # "Each instance of TestCase will run a single test method: the |
| 68 | # method named methodName." |
| 69 | # ... |
| 70 | # "methodName defaults to "runTest"." |
| 71 | # |
| 72 | # Make sure it really is optional, and that it defaults to the proper |
| 73 | # thing. |
| 74 | def test_init__no_test_name(self): |
| 75 | class Test(unittest.TestCase): |
| 76 | def runTest(self): raise MyException() |
| 77 | def test(self): pass |
| 78 | |
| 79 | self.assertEqual(Test().id()[-13:], '.Test.runTest') |
| 80 | |
Michael Foord | 32e1d83 | 2011-01-03 17:00:11 +0000 | [diff] [blame] | 81 | # test that TestCase can be instantiated with no args |
| 82 | # primarily for use at the interactive interpreter |
| 83 | test = unittest.TestCase() |
| 84 | test.assertEqual(3, 3) |
| 85 | with test.assertRaises(test.failureException): |
| 86 | test.assertEqual(3, 2) |
| 87 | |
| 88 | with self.assertRaises(AttributeError): |
| 89 | test.run() |
| 90 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 91 | # "class TestCase([methodName])" |
| 92 | # ... |
| 93 | # "Each instance of TestCase will run a single test method: the |
| 94 | # method named methodName." |
| 95 | def test_init__test_name__valid(self): |
| 96 | class Test(unittest.TestCase): |
| 97 | def runTest(self): raise MyException() |
| 98 | def test(self): pass |
| 99 | |
| 100 | self.assertEqual(Test('test').id()[-10:], '.Test.test') |
| 101 | |
| 102 | # "class TestCase([methodName])" |
| 103 | # ... |
| 104 | # "Each instance of TestCase will run a single test method: the |
| 105 | # method named methodName." |
| 106 | def test_init__test_name__invalid(self): |
| 107 | class Test(unittest.TestCase): |
| 108 | def runTest(self): raise MyException() |
| 109 | def test(self): pass |
| 110 | |
| 111 | try: |
| 112 | Test('testfoo') |
| 113 | except ValueError: |
| 114 | pass |
| 115 | else: |
| 116 | self.fail("Failed to raise ValueError") |
| 117 | |
| 118 | # "Return the number of tests represented by the this test object. For |
| 119 | # TestCase instances, this will always be 1" |
| 120 | def test_countTestCases(self): |
| 121 | class Foo(unittest.TestCase): |
| 122 | def test(self): pass |
| 123 | |
| 124 | self.assertEqual(Foo('test').countTestCases(), 1) |
| 125 | |
| 126 | # "Return the default type of test result object to be used to run this |
| 127 | # test. For TestCase instances, this will always be |
| 128 | # unittest.TestResult; subclasses of TestCase should |
| 129 | # override this as necessary." |
| 130 | def test_defaultTestResult(self): |
| 131 | class Foo(unittest.TestCase): |
| 132 | def runTest(self): |
| 133 | pass |
| 134 | |
| 135 | result = Foo().defaultTestResult() |
| 136 | self.assertEqual(type(result), unittest.TestResult) |
| 137 | |
| 138 | # "When a setUp() method is defined, the test runner will run that method |
| 139 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 140 | # test runner will invoke that method after each test. In the example, |
| 141 | # setUp() was used to create a fresh sequence for each test." |
| 142 | # |
| 143 | # Make sure the proper call order is maintained, even if setUp() raises |
| 144 | # an exception. |
| 145 | def test_run_call_order__error_in_setUp(self): |
| 146 | events = [] |
| 147 | result = LoggingResult(events) |
| 148 | |
| 149 | class Foo(Test.LoggingTestCase): |
| 150 | def setUp(self): |
| 151 | super(Foo, self).setUp() |
| 152 | raise RuntimeError('raised by Foo.setUp') |
| 153 | |
| 154 | Foo(events).run(result) |
| 155 | expected = ['startTest', 'setUp', 'addError', 'stopTest'] |
| 156 | self.assertEqual(events, expected) |
| 157 | |
| 158 | # "With a temporary result stopTestRun is called when setUp errors. |
| 159 | def test_run_call_order__error_in_setUp_default_result(self): |
| 160 | events = [] |
| 161 | |
| 162 | class Foo(Test.LoggingTestCase): |
| 163 | def defaultTestResult(self): |
| 164 | return LoggingResult(self.events) |
| 165 | |
| 166 | def setUp(self): |
| 167 | super(Foo, self).setUp() |
| 168 | raise RuntimeError('raised by Foo.setUp') |
| 169 | |
| 170 | Foo(events).run() |
| 171 | expected = ['startTestRun', 'startTest', 'setUp', 'addError', |
| 172 | 'stopTest', 'stopTestRun'] |
| 173 | self.assertEqual(events, expected) |
| 174 | |
| 175 | # "When a setUp() method is defined, the test runner will run that method |
| 176 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 177 | # test runner will invoke that method after each test. In the example, |
| 178 | # setUp() was used to create a fresh sequence for each test." |
| 179 | # |
| 180 | # Make sure the proper call order is maintained, even if the test raises |
| 181 | # an error (as opposed to a failure). |
| 182 | def test_run_call_order__error_in_test(self): |
| 183 | events = [] |
| 184 | result = LoggingResult(events) |
| 185 | |
| 186 | class Foo(Test.LoggingTestCase): |
| 187 | def test(self): |
| 188 | super(Foo, self).test() |
| 189 | raise RuntimeError('raised by Foo.test') |
| 190 | |
Michael Foord | b3468f7 | 2010-12-19 03:19:47 +0000 | [diff] [blame] | 191 | expected = ['startTest', 'setUp', 'test', 'tearDown', |
| 192 | 'addError', 'stopTest'] |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 193 | Foo(events).run(result) |
| 194 | self.assertEqual(events, expected) |
| 195 | |
| 196 | # "With a default result, an error in the test still results in stopTestRun |
| 197 | # being called." |
| 198 | def test_run_call_order__error_in_test_default_result(self): |
| 199 | events = [] |
| 200 | |
| 201 | class Foo(Test.LoggingTestCase): |
| 202 | def defaultTestResult(self): |
| 203 | return LoggingResult(self.events) |
| 204 | |
| 205 | def test(self): |
| 206 | super(Foo, self).test() |
| 207 | raise RuntimeError('raised by Foo.test') |
| 208 | |
Michael Foord | b3468f7 | 2010-12-19 03:19:47 +0000 | [diff] [blame] | 209 | expected = ['startTestRun', 'startTest', 'setUp', 'test', |
| 210 | 'tearDown', 'addError', 'stopTest', 'stopTestRun'] |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 211 | Foo(events).run() |
| 212 | self.assertEqual(events, expected) |
| 213 | |
| 214 | # "When a setUp() method is defined, the test runner will run that method |
| 215 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 216 | # test runner will invoke that method after each test. In the example, |
| 217 | # setUp() was used to create a fresh sequence for each test." |
| 218 | # |
| 219 | # Make sure the proper call order is maintained, even if the test signals |
| 220 | # a failure (as opposed to an error). |
| 221 | def test_run_call_order__failure_in_test(self): |
| 222 | events = [] |
| 223 | result = LoggingResult(events) |
| 224 | |
| 225 | class Foo(Test.LoggingTestCase): |
| 226 | def test(self): |
| 227 | super(Foo, self).test() |
| 228 | self.fail('raised by Foo.test') |
| 229 | |
Michael Foord | b3468f7 | 2010-12-19 03:19:47 +0000 | [diff] [blame] | 230 | expected = ['startTest', 'setUp', 'test', 'tearDown', |
| 231 | 'addFailure', 'stopTest'] |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 232 | Foo(events).run(result) |
| 233 | self.assertEqual(events, expected) |
| 234 | |
| 235 | # "When a test fails with a default result stopTestRun is still called." |
| 236 | def test_run_call_order__failure_in_test_default_result(self): |
| 237 | |
| 238 | class Foo(Test.LoggingTestCase): |
| 239 | def defaultTestResult(self): |
| 240 | return LoggingResult(self.events) |
| 241 | def test(self): |
| 242 | super(Foo, self).test() |
| 243 | self.fail('raised by Foo.test') |
| 244 | |
Michael Foord | b3468f7 | 2010-12-19 03:19:47 +0000 | [diff] [blame] | 245 | expected = ['startTestRun', 'startTest', 'setUp', 'test', |
| 246 | 'tearDown', 'addFailure', 'stopTest', 'stopTestRun'] |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 247 | events = [] |
| 248 | Foo(events).run() |
| 249 | self.assertEqual(events, expected) |
| 250 | |
| 251 | # "When a setUp() method is defined, the test runner will run that method |
| 252 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 253 | # test runner will invoke that method after each test. In the example, |
| 254 | # setUp() was used to create a fresh sequence for each test." |
| 255 | # |
| 256 | # Make sure the proper call order is maintained, even if tearDown() raises |
| 257 | # an exception. |
| 258 | def test_run_call_order__error_in_tearDown(self): |
| 259 | events = [] |
| 260 | result = LoggingResult(events) |
| 261 | |
| 262 | class Foo(Test.LoggingTestCase): |
| 263 | def tearDown(self): |
| 264 | super(Foo, self).tearDown() |
| 265 | raise RuntimeError('raised by Foo.tearDown') |
| 266 | |
| 267 | Foo(events).run(result) |
| 268 | expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError', |
| 269 | 'stopTest'] |
| 270 | self.assertEqual(events, expected) |
| 271 | |
| 272 | # "When tearDown errors with a default result stopTestRun is still called." |
| 273 | def test_run_call_order__error_in_tearDown_default_result(self): |
| 274 | |
| 275 | class Foo(Test.LoggingTestCase): |
| 276 | def defaultTestResult(self): |
| 277 | return LoggingResult(self.events) |
| 278 | def tearDown(self): |
| 279 | super(Foo, self).tearDown() |
| 280 | raise RuntimeError('raised by Foo.tearDown') |
| 281 | |
| 282 | events = [] |
| 283 | Foo(events).run() |
| 284 | expected = ['startTestRun', 'startTest', 'setUp', 'test', 'tearDown', |
| 285 | 'addError', 'stopTest', 'stopTestRun'] |
| 286 | self.assertEqual(events, expected) |
| 287 | |
| 288 | # "TestCase.run() still works when the defaultTestResult is a TestResult |
| 289 | # that does not support startTestRun and stopTestRun. |
| 290 | def test_run_call_order_default_result(self): |
| 291 | |
| 292 | class Foo(unittest.TestCase): |
| 293 | def defaultTestResult(self): |
| 294 | return ResultWithNoStartTestRunStopTestRun() |
| 295 | def test(self): |
| 296 | pass |
| 297 | |
| 298 | Foo('test').run() |
| 299 | |
Antoine Pitrou | c9b3ef2 | 2013-03-20 20:16:47 +0100 | [diff] [blame] | 300 | def _check_call_order__subtests(self, result, events, expected_events): |
| 301 | class Foo(Test.LoggingTestCase): |
| 302 | def test(self): |
| 303 | super(Foo, self).test() |
| 304 | for i in [1, 2, 3]: |
| 305 | with self.subTest(i=i): |
| 306 | if i == 1: |
| 307 | self.fail('failure') |
| 308 | for j in [2, 3]: |
| 309 | with self.subTest(j=j): |
| 310 | if i * j == 6: |
| 311 | raise RuntimeError('raised by Foo.test') |
| 312 | 1 / 0 |
| 313 | |
| 314 | # Order is the following: |
| 315 | # i=1 => subtest failure |
| 316 | # i=2, j=2 => subtest success |
| 317 | # i=2, j=3 => subtest error |
| 318 | # i=3, j=2 => subtest error |
| 319 | # i=3, j=3 => subtest success |
| 320 | # toplevel => error |
| 321 | Foo(events).run(result) |
| 322 | self.assertEqual(events, expected_events) |
| 323 | |
| 324 | def test_run_call_order__subtests(self): |
| 325 | events = [] |
| 326 | result = LoggingResult(events) |
| 327 | expected = ['startTest', 'setUp', 'test', 'tearDown', |
| 328 | 'addSubTestFailure', 'addSubTestSuccess', |
| 329 | 'addSubTestFailure', 'addSubTestFailure', |
| 330 | 'addSubTestSuccess', 'addError', 'stopTest'] |
| 331 | self._check_call_order__subtests(result, events, expected) |
| 332 | |
| 333 | def test_run_call_order__subtests_legacy(self): |
| 334 | # With a legacy result object (without a addSubTest method), |
| 335 | # text execution stops after the first subtest failure. |
| 336 | events = [] |
| 337 | result = LegacyLoggingResult(events) |
| 338 | expected = ['startTest', 'setUp', 'test', 'tearDown', |
| 339 | 'addFailure', 'stopTest'] |
| 340 | self._check_call_order__subtests(result, events, expected) |
| 341 | |
| 342 | def _check_call_order__subtests_success(self, result, events, expected_events): |
| 343 | class Foo(Test.LoggingTestCase): |
| 344 | def test(self): |
| 345 | super(Foo, self).test() |
| 346 | for i in [1, 2]: |
| 347 | with self.subTest(i=i): |
| 348 | for j in [2, 3]: |
| 349 | with self.subTest(j=j): |
| 350 | pass |
| 351 | |
| 352 | Foo(events).run(result) |
| 353 | self.assertEqual(events, expected_events) |
| 354 | |
| 355 | def test_run_call_order__subtests_success(self): |
| 356 | events = [] |
| 357 | result = LoggingResult(events) |
| 358 | # The 6 subtest successes are individually recorded, in addition |
| 359 | # to the whole test success. |
| 360 | expected = (['startTest', 'setUp', 'test', 'tearDown'] |
| 361 | + 6 * ['addSubTestSuccess'] |
| 362 | + ['addSuccess', 'stopTest']) |
| 363 | self._check_call_order__subtests_success(result, events, expected) |
| 364 | |
| 365 | def test_run_call_order__subtests_success_legacy(self): |
| 366 | # With a legacy result, only the whole test success is recorded. |
| 367 | events = [] |
| 368 | result = LegacyLoggingResult(events) |
| 369 | expected = ['startTest', 'setUp', 'test', 'tearDown', |
| 370 | 'addSuccess', 'stopTest'] |
| 371 | self._check_call_order__subtests_success(result, events, expected) |
| 372 | |
| 373 | def test_run_call_order__subtests_failfast(self): |
| 374 | events = [] |
| 375 | result = LoggingResult(events) |
| 376 | result.failfast = True |
| 377 | |
| 378 | class Foo(Test.LoggingTestCase): |
| 379 | def test(self): |
| 380 | super(Foo, self).test() |
| 381 | with self.subTest(i=1): |
| 382 | self.fail('failure') |
| 383 | with self.subTest(i=2): |
| 384 | self.fail('failure') |
| 385 | self.fail('failure') |
| 386 | |
| 387 | expected = ['startTest', 'setUp', 'test', 'tearDown', |
| 388 | 'addSubTestFailure', 'stopTest'] |
| 389 | Foo(events).run(result) |
| 390 | self.assertEqual(events, expected) |
| 391 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 392 | # "This class attribute gives the exception raised by the test() method. |
| 393 | # If a test framework needs to use a specialized exception, possibly to |
| 394 | # carry additional information, it must subclass this exception in |
| 395 | # order to ``play fair'' with the framework. The initial value of this |
| 396 | # attribute is AssertionError" |
| 397 | def test_failureException__default(self): |
| 398 | class Foo(unittest.TestCase): |
| 399 | def test(self): |
| 400 | pass |
| 401 | |
| 402 | self.assertTrue(Foo('test').failureException is AssertionError) |
| 403 | |
| 404 | # "This class attribute gives the exception raised by the test() method. |
| 405 | # If a test framework needs to use a specialized exception, possibly to |
| 406 | # carry additional information, it must subclass this exception in |
| 407 | # order to ``play fair'' with the framework." |
| 408 | # |
| 409 | # Make sure TestCase.run() respects the designated failureException |
| 410 | def test_failureException__subclassing__explicit_raise(self): |
| 411 | events = [] |
| 412 | result = LoggingResult(events) |
| 413 | |
| 414 | class Foo(unittest.TestCase): |
| 415 | def test(self): |
| 416 | raise RuntimeError() |
| 417 | |
| 418 | failureException = RuntimeError |
| 419 | |
| 420 | self.assertTrue(Foo('test').failureException is RuntimeError) |
| 421 | |
| 422 | |
| 423 | Foo('test').run(result) |
| 424 | expected = ['startTest', 'addFailure', 'stopTest'] |
| 425 | self.assertEqual(events, expected) |
| 426 | |
| 427 | # "This class attribute gives the exception raised by the test() method. |
| 428 | # If a test framework needs to use a specialized exception, possibly to |
| 429 | # carry additional information, it must subclass this exception in |
| 430 | # order to ``play fair'' with the framework." |
| 431 | # |
| 432 | # Make sure TestCase.run() respects the designated failureException |
| 433 | def test_failureException__subclassing__implicit_raise(self): |
| 434 | events = [] |
| 435 | result = LoggingResult(events) |
| 436 | |
| 437 | class Foo(unittest.TestCase): |
| 438 | def test(self): |
| 439 | self.fail("foo") |
| 440 | |
| 441 | failureException = RuntimeError |
| 442 | |
| 443 | self.assertTrue(Foo('test').failureException is RuntimeError) |
| 444 | |
| 445 | |
| 446 | Foo('test').run(result) |
| 447 | expected = ['startTest', 'addFailure', 'stopTest'] |
| 448 | self.assertEqual(events, expected) |
| 449 | |
| 450 | # "The default implementation does nothing." |
| 451 | def test_setUp(self): |
| 452 | class Foo(unittest.TestCase): |
| 453 | def runTest(self): |
| 454 | pass |
| 455 | |
| 456 | # ... and nothing should happen |
| 457 | Foo().setUp() |
| 458 | |
| 459 | # "The default implementation does nothing." |
| 460 | def test_tearDown(self): |
| 461 | class Foo(unittest.TestCase): |
| 462 | def runTest(self): |
| 463 | pass |
| 464 | |
| 465 | # ... and nothing should happen |
| 466 | Foo().tearDown() |
| 467 | |
| 468 | # "Return a string identifying the specific test case." |
| 469 | # |
| 470 | # Because of the vague nature of the docs, I'm not going to lock this |
| 471 | # test down too much. Really all that can be asserted is that the id() |
| 472 | # will be a string (either 8-byte or unicode -- again, because the docs |
| 473 | # just say "string") |
| 474 | def test_id(self): |
| 475 | class Foo(unittest.TestCase): |
| 476 | def runTest(self): |
| 477 | pass |
| 478 | |
| 479 | self.assertIsInstance(Foo().id(), str) |
| 480 | |
| 481 | |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 482 | # "If result is omitted or None, a temporary result object is created, |
| 483 | # used, and is made available to the caller. As TestCase owns the |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 484 | # temporary result startTestRun and stopTestRun are called. |
| 485 | |
| 486 | def test_run__uses_defaultTestResult(self): |
| 487 | events = [] |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 488 | defaultResult = LoggingResult(events) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 489 | |
| 490 | class Foo(unittest.TestCase): |
| 491 | def test(self): |
| 492 | events.append('test') |
| 493 | |
| 494 | def defaultTestResult(self): |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 495 | return defaultResult |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 496 | |
| 497 | # Make run() find a result object on its own |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 498 | result = Foo('test').run() |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 499 | |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 500 | self.assertIs(result, defaultResult) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 501 | expected = ['startTestRun', 'startTest', 'test', 'addSuccess', |
| 502 | 'stopTest', 'stopTestRun'] |
| 503 | self.assertEqual(events, expected) |
| 504 | |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 505 | |
| 506 | # "The result object is returned to run's caller" |
| 507 | def test_run__returns_given_result(self): |
| 508 | |
| 509 | class Foo(unittest.TestCase): |
| 510 | def test(self): |
| 511 | pass |
| 512 | |
| 513 | result = unittest.TestResult() |
| 514 | |
| 515 | retval = Foo('test').run(result) |
| 516 | self.assertIs(retval, result) |
| 517 | |
| 518 | |
| 519 | # "The same effect [as method run] may be had by simply calling the |
| 520 | # TestCase instance." |
| 521 | def test_call__invoking_an_instance_delegates_to_run(self): |
| 522 | resultIn = unittest.TestResult() |
| 523 | resultOut = unittest.TestResult() |
| 524 | |
| 525 | class Foo(unittest.TestCase): |
| 526 | def test(self): |
| 527 | pass |
| 528 | |
| 529 | def run(self, result): |
| 530 | self.assertIs(result, resultIn) |
| 531 | return resultOut |
| 532 | |
| 533 | retval = Foo('test')(resultIn) |
| 534 | |
| 535 | self.assertIs(retval, resultOut) |
| 536 | |
| 537 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 538 | def testShortDescriptionWithoutDocstring(self): |
| 539 | self.assertIsNone(self.shortDescription()) |
| 540 | |
| 541 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 542 | "Docstrings are omitted with -O2 and above") |
| 543 | def testShortDescriptionWithOneLineDocstring(self): |
| 544 | """Tests shortDescription() for a method with a docstring.""" |
| 545 | self.assertEqual( |
| 546 | self.shortDescription(), |
| 547 | 'Tests shortDescription() for a method with a docstring.') |
| 548 | |
| 549 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 550 | "Docstrings are omitted with -O2 and above") |
| 551 | def testShortDescriptionWithMultiLineDocstring(self): |
| 552 | """Tests shortDescription() for a method with a longer docstring. |
| 553 | |
| 554 | This method ensures that only the first line of a docstring is |
| 555 | returned used in the short description, no matter how long the |
| 556 | whole thing is. |
| 557 | """ |
| 558 | self.assertEqual( |
| 559 | self.shortDescription(), |
| 560 | 'Tests shortDescription() for a method with a longer ' |
| 561 | 'docstring.') |
| 562 | |
| 563 | def testAddTypeEqualityFunc(self): |
| 564 | class SadSnake(object): |
| 565 | """Dummy class for test_addTypeEqualityFunc.""" |
| 566 | s1, s2 = SadSnake(), SadSnake() |
| 567 | self.assertFalse(s1 == s2) |
| 568 | def AllSnakesCreatedEqual(a, b, msg=None): |
| 569 | return type(a) == type(b) == SadSnake |
| 570 | self.addTypeEqualityFunc(SadSnake, AllSnakesCreatedEqual) |
| 571 | self.assertEqual(s1, s2) |
| 572 | # No this doesn't clean up and remove the SadSnake equality func |
| 573 | # from this TestCase instance but since its a local nothing else |
| 574 | # will ever notice that. |
| 575 | |
| 576 | def testAssertIs(self): |
| 577 | thing = object() |
| 578 | self.assertIs(thing, thing) |
| 579 | self.assertRaises(self.failureException, self.assertIs, thing, object()) |
| 580 | |
| 581 | def testAssertIsNot(self): |
| 582 | thing = object() |
| 583 | self.assertIsNot(thing, object()) |
| 584 | self.assertRaises(self.failureException, self.assertIsNot, thing, thing) |
| 585 | |
| 586 | def testAssertIsInstance(self): |
| 587 | thing = [] |
| 588 | self.assertIsInstance(thing, list) |
| 589 | self.assertRaises(self.failureException, self.assertIsInstance, |
| 590 | thing, dict) |
| 591 | |
| 592 | def testAssertNotIsInstance(self): |
| 593 | thing = [] |
| 594 | self.assertNotIsInstance(thing, dict) |
| 595 | self.assertRaises(self.failureException, self.assertNotIsInstance, |
| 596 | thing, list) |
| 597 | |
| 598 | def testAssertIn(self): |
| 599 | animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'} |
| 600 | |
| 601 | self.assertIn('a', 'abc') |
| 602 | self.assertIn(2, [1, 2, 3]) |
| 603 | self.assertIn('monkey', animals) |
| 604 | |
| 605 | self.assertNotIn('d', 'abc') |
| 606 | self.assertNotIn(0, [1, 2, 3]) |
| 607 | self.assertNotIn('otter', animals) |
| 608 | |
| 609 | self.assertRaises(self.failureException, self.assertIn, 'x', 'abc') |
| 610 | self.assertRaises(self.failureException, self.assertIn, 4, [1, 2, 3]) |
| 611 | self.assertRaises(self.failureException, self.assertIn, 'elephant', |
| 612 | animals) |
| 613 | |
| 614 | self.assertRaises(self.failureException, self.assertNotIn, 'c', 'abc') |
| 615 | self.assertRaises(self.failureException, self.assertNotIn, 1, [1, 2, 3]) |
| 616 | self.assertRaises(self.failureException, self.assertNotIn, 'cow', |
| 617 | animals) |
| 618 | |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 619 | def testAssertDictContainsSubset(self): |
| 620 | with warnings.catch_warnings(): |
| 621 | warnings.simplefilter("ignore", DeprecationWarning) |
| 622 | |
| 623 | self.assertDictContainsSubset({}, {}) |
| 624 | self.assertDictContainsSubset({}, {'a': 1}) |
| 625 | self.assertDictContainsSubset({'a': 1}, {'a': 1}) |
| 626 | self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2}) |
| 627 | self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2}) |
| 628 | |
| 629 | with self.assertRaises(self.failureException): |
| 630 | self.assertDictContainsSubset({1: "one"}, {}) |
| 631 | |
| 632 | with self.assertRaises(self.failureException): |
| 633 | self.assertDictContainsSubset({'a': 2}, {'a': 1}) |
| 634 | |
| 635 | with self.assertRaises(self.failureException): |
| 636 | self.assertDictContainsSubset({'c': 1}, {'a': 1}) |
| 637 | |
| 638 | with self.assertRaises(self.failureException): |
| 639 | self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1}) |
| 640 | |
| 641 | with self.assertRaises(self.failureException): |
| 642 | self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1}) |
| 643 | |
| 644 | one = ''.join(chr(i) for i in range(255)) |
| 645 | # this used to cause a UnicodeDecodeError constructing the failure msg |
| 646 | with self.assertRaises(self.failureException): |
| 647 | self.assertDictContainsSubset({'foo': one}, {'foo': '\uFFFD'}) |
| 648 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 649 | def testAssertEqual(self): |
| 650 | equal_pairs = [ |
| 651 | ((), ()), |
| 652 | ({}, {}), |
| 653 | ([], []), |
| 654 | (set(), set()), |
| 655 | (frozenset(), frozenset())] |
| 656 | for a, b in equal_pairs: |
| 657 | # This mess of try excepts is to test the assertEqual behavior |
| 658 | # itself. |
| 659 | try: |
| 660 | self.assertEqual(a, b) |
| 661 | except self.failureException: |
| 662 | self.fail('assertEqual(%r, %r) failed' % (a, b)) |
| 663 | try: |
| 664 | self.assertEqual(a, b, msg='foo') |
| 665 | except self.failureException: |
| 666 | self.fail('assertEqual(%r, %r) with msg= failed' % (a, b)) |
| 667 | try: |
| 668 | self.assertEqual(a, b, 'foo') |
| 669 | except self.failureException: |
| 670 | self.fail('assertEqual(%r, %r) with third parameter failed' % |
| 671 | (a, b)) |
| 672 | |
| 673 | unequal_pairs = [ |
| 674 | ((), []), |
| 675 | ({}, set()), |
| 676 | (set([4,1]), frozenset([4,2])), |
| 677 | (frozenset([4,5]), set([2,3])), |
| 678 | (set([3,4]), set([5,4]))] |
| 679 | for a, b in unequal_pairs: |
| 680 | self.assertRaises(self.failureException, self.assertEqual, a, b) |
| 681 | self.assertRaises(self.failureException, self.assertEqual, a, b, |
| 682 | 'foo') |
| 683 | self.assertRaises(self.failureException, self.assertEqual, a, b, |
| 684 | msg='foo') |
| 685 | |
| 686 | def testEquality(self): |
| 687 | self.assertListEqual([], []) |
| 688 | self.assertTupleEqual((), ()) |
| 689 | self.assertSequenceEqual([], ()) |
| 690 | |
| 691 | a = [0, 'a', []] |
| 692 | b = [] |
| 693 | self.assertRaises(unittest.TestCase.failureException, |
| 694 | self.assertListEqual, a, b) |
| 695 | self.assertRaises(unittest.TestCase.failureException, |
| 696 | self.assertListEqual, tuple(a), tuple(b)) |
| 697 | self.assertRaises(unittest.TestCase.failureException, |
| 698 | self.assertSequenceEqual, a, tuple(b)) |
| 699 | |
| 700 | b.extend(a) |
| 701 | self.assertListEqual(a, b) |
| 702 | self.assertTupleEqual(tuple(a), tuple(b)) |
| 703 | self.assertSequenceEqual(a, tuple(b)) |
| 704 | self.assertSequenceEqual(tuple(a), b) |
| 705 | |
| 706 | self.assertRaises(self.failureException, self.assertListEqual, |
| 707 | a, tuple(b)) |
| 708 | self.assertRaises(self.failureException, self.assertTupleEqual, |
| 709 | tuple(a), b) |
| 710 | self.assertRaises(self.failureException, self.assertListEqual, None, b) |
| 711 | self.assertRaises(self.failureException, self.assertTupleEqual, None, |
| 712 | tuple(b)) |
| 713 | self.assertRaises(self.failureException, self.assertSequenceEqual, |
| 714 | None, tuple(b)) |
| 715 | self.assertRaises(self.failureException, self.assertListEqual, 1, 1) |
| 716 | self.assertRaises(self.failureException, self.assertTupleEqual, 1, 1) |
| 717 | self.assertRaises(self.failureException, self.assertSequenceEqual, |
| 718 | 1, 1) |
| 719 | |
| 720 | self.assertDictEqual({}, {}) |
| 721 | |
| 722 | c = { 'x': 1 } |
| 723 | d = {} |
| 724 | self.assertRaises(unittest.TestCase.failureException, |
| 725 | self.assertDictEqual, c, d) |
| 726 | |
| 727 | d.update(c) |
| 728 | self.assertDictEqual(c, d) |
| 729 | |
| 730 | d['x'] = 0 |
| 731 | self.assertRaises(unittest.TestCase.failureException, |
| 732 | self.assertDictEqual, c, d, 'These are unequal') |
| 733 | |
| 734 | self.assertRaises(self.failureException, self.assertDictEqual, None, d) |
| 735 | self.assertRaises(self.failureException, self.assertDictEqual, [], d) |
| 736 | self.assertRaises(self.failureException, self.assertDictEqual, 1, 1) |
| 737 | |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 738 | def testAssertSequenceEqualMaxDiff(self): |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 739 | self.assertEqual(self.maxDiff, 80*8) |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 740 | seq1 = 'a' + 'x' * 80**2 |
| 741 | seq2 = 'b' + 'x' * 80**2 |
| 742 | diff = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(), |
| 743 | pprint.pformat(seq2).splitlines())) |
Michael Foord | da562f6 | 2010-06-05 21:01:08 +0000 | [diff] [blame] | 744 | # the +1 is the leading \n added by assertSequenceEqual |
| 745 | omitted = unittest.case.DIFF_OMITTED % (len(diff) + 1,) |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 746 | |
| 747 | self.maxDiff = len(diff)//2 |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 748 | try: |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 749 | |
| 750 | self.assertSequenceEqual(seq1, seq2) |
| 751 | except self.failureException as e: |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 752 | msg = e.args[0] |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 753 | else: |
| 754 | self.fail('assertSequenceEqual did not fail.') |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 755 | self.assertTrue(len(msg) < len(diff)) |
Michael Foord | da562f6 | 2010-06-05 21:01:08 +0000 | [diff] [blame] | 756 | self.assertIn(omitted, msg) |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 757 | |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 758 | self.maxDiff = len(diff) * 2 |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 759 | try: |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 760 | self.assertSequenceEqual(seq1, seq2) |
| 761 | except self.failureException as e: |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 762 | msg = e.args[0] |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 763 | else: |
| 764 | self.fail('assertSequenceEqual did not fail.') |
| 765 | self.assertTrue(len(msg) > len(diff)) |
Michael Foord | da562f6 | 2010-06-05 21:01:08 +0000 | [diff] [blame] | 766 | self.assertNotIn(omitted, msg) |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 767 | |
| 768 | self.maxDiff = None |
| 769 | try: |
| 770 | self.assertSequenceEqual(seq1, seq2) |
| 771 | except self.failureException as e: |
| 772 | msg = e.args[0] |
| 773 | else: |
| 774 | self.fail('assertSequenceEqual did not fail.') |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 775 | self.assertTrue(len(msg) > len(diff)) |
Michael Foord | da562f6 | 2010-06-05 21:01:08 +0000 | [diff] [blame] | 776 | self.assertNotIn(omitted, msg) |
| 777 | |
| 778 | def testTruncateMessage(self): |
| 779 | self.maxDiff = 1 |
| 780 | message = self._truncateMessage('foo', 'bar') |
| 781 | omitted = unittest.case.DIFF_OMITTED % len('bar') |
| 782 | self.assertEqual(message, 'foo' + omitted) |
| 783 | |
| 784 | self.maxDiff = None |
| 785 | message = self._truncateMessage('foo', 'bar') |
| 786 | self.assertEqual(message, 'foobar') |
| 787 | |
Michael Foord | 520ed0a | 2010-06-05 21:12:23 +0000 | [diff] [blame] | 788 | self.maxDiff = 4 |
| 789 | message = self._truncateMessage('foo', 'bar') |
| 790 | self.assertEqual(message, 'foobar') |
| 791 | |
Michael Foord | da562f6 | 2010-06-05 21:01:08 +0000 | [diff] [blame] | 792 | def testAssertDictEqualTruncates(self): |
| 793 | test = unittest.TestCase('assertEqual') |
| 794 | def truncate(msg, diff): |
| 795 | return 'foo' |
| 796 | test._truncateMessage = truncate |
| 797 | try: |
| 798 | test.assertDictEqual({}, {1: 0}) |
| 799 | except self.failureException as e: |
| 800 | self.assertEqual(str(e), 'foo') |
| 801 | else: |
| 802 | self.fail('assertDictEqual did not fail') |
| 803 | |
| 804 | def testAssertMultiLineEqualTruncates(self): |
| 805 | test = unittest.TestCase('assertEqual') |
| 806 | def truncate(msg, diff): |
| 807 | return 'foo' |
| 808 | test._truncateMessage = truncate |
| 809 | try: |
| 810 | test.assertMultiLineEqual('foo', 'bar') |
| 811 | except self.failureException as e: |
| 812 | self.assertEqual(str(e), 'foo') |
| 813 | else: |
| 814 | self.fail('assertMultiLineEqual did not fail') |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 815 | |
Ezio Melotti | edd117f | 2011-04-27 10:20:38 +0300 | [diff] [blame] | 816 | def testAssertEqual_diffThreshold(self): |
| 817 | # check threshold value |
| 818 | self.assertEqual(self._diffThreshold, 2**16) |
| 819 | # disable madDiff to get diff markers |
| 820 | self.maxDiff = None |
| 821 | |
| 822 | # set a lower threshold value and add a cleanup to restore it |
| 823 | old_threshold = self._diffThreshold |
| 824 | self._diffThreshold = 2**8 |
| 825 | self.addCleanup(lambda: setattr(self, '_diffThreshold', old_threshold)) |
| 826 | |
| 827 | # under the threshold: diff marker (^) in error message |
| 828 | s = 'x' * (2**7) |
| 829 | with self.assertRaises(self.failureException) as cm: |
| 830 | self.assertEqual(s + 'a', s + 'b') |
| 831 | self.assertIn('^', str(cm.exception)) |
| 832 | self.assertEqual(s + 'a', s + 'a') |
| 833 | |
| 834 | # over the threshold: diff not used and marker (^) not in error message |
| 835 | s = 'x' * (2**9) |
| 836 | # if the path that uses difflib is taken, _truncateMessage will be |
| 837 | # called -- replace it with explodingTruncation to verify that this |
| 838 | # doesn't happen |
| 839 | def explodingTruncation(message, diff): |
| 840 | raise SystemError('this should not be raised') |
| 841 | old_truncate = self._truncateMessage |
| 842 | self._truncateMessage = explodingTruncation |
| 843 | self.addCleanup(lambda: setattr(self, '_truncateMessage', old_truncate)) |
| 844 | |
| 845 | s1, s2 = s + 'a', s + 'b' |
| 846 | with self.assertRaises(self.failureException) as cm: |
| 847 | self.assertEqual(s1, s2) |
| 848 | self.assertNotIn('^', str(cm.exception)) |
| 849 | self.assertEqual(str(cm.exception), '%r != %r' % (s1, s2)) |
| 850 | self.assertEqual(s + 'a', s + 'a') |
| 851 | |
Raymond Hettinger | f954217 | 2010-12-24 10:30:06 +0000 | [diff] [blame] | 852 | def testAssertCountEqual(self): |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 853 | a = object() |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 854 | self.assertCountEqual([1, 2, 3], [3, 2, 1]) |
| 855 | self.assertCountEqual(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo']) |
| 856 | self.assertCountEqual([a, a, 2, 2, 3], (a, 2, 3, a, 2)) |
| 857 | self.assertCountEqual([1, "2", "a", "a"], ["a", "2", True, "a"]) |
| 858 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 859 | [1, 2] + [3] * 100, [1] * 100 + [2, 3]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 860 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 861 | [1, "2", "a", "a"], ["a", "2", True, 1]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 862 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 863 | [10], [10, 11]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 864 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 865 | [10, 11], [10]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 866 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 867 | [10, 11, 10], [10, 11]) |
| 868 | |
| 869 | # Test that sequences of unhashable objects can be tested for sameness: |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 870 | self.assertCountEqual([[1, 2], [3, 4], 0], [False, [3, 4], [1, 2]]) |
Raymond Hettinger | 8396124 | 2010-12-24 00:48:47 +0000 | [diff] [blame] | 871 | # Test that iterator of unhashable objects can be tested for sameness: |
| 872 | self.assertCountEqual(iter([1, 2, [], 3, 4]), |
| 873 | iter([1, 2, [], 3, 4])) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 874 | |
| 875 | # hashable types, but not orderable |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 876 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 877 | [], [divmod, 'x', 1, 5j, 2j, frozenset()]) |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 878 | # comparing dicts |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 879 | self.assertCountEqual([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}]) |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 880 | # comparing heterogenous non-hashable sequences |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 881 | self.assertCountEqual([1, 'x', divmod, []], [divmod, [], 'x', 1]) |
| 882 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 883 | [], [divmod, [], 'x', 1, 5j, 2j, set()]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 884 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 885 | [[1]], [[2]]) |
| 886 | |
| 887 | # Same elements, but not same sequence length |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 888 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 889 | [1, 1, 2], [2, 1]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 890 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 891 | [1, 1, "2", "a", "a"], ["2", "2", True, "a"]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 892 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 893 | [1, {'b': 2}, None, True], [{'b': 2}, True, None]) |
| 894 | |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 895 | # Same elements which don't reliably compare, in |
| 896 | # different order, see issue 10242 |
| 897 | a = [{2,4}, {1,2}] |
| 898 | b = a[::-1] |
| 899 | self.assertCountEqual(a, b) |
| 900 | |
Raymond Hettinger | f954217 | 2010-12-24 10:30:06 +0000 | [diff] [blame] | 901 | # test utility functions supporting assertCountEqual() |
| 902 | |
| 903 | diffs = set(unittest.util._count_diff_all_purpose('aaabccd', 'abbbcce')) |
| 904 | expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')} |
| 905 | self.assertEqual(diffs, expected) |
| 906 | |
| 907 | diffs = unittest.util._count_diff_all_purpose([[]], []) |
| 908 | self.assertEqual(diffs, [(1, 0, [])]) |
| 909 | |
| 910 | diffs = set(unittest.util._count_diff_hashable('aaabccd', 'abbbcce')) |
| 911 | expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')} |
| 912 | self.assertEqual(diffs, expected) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 913 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 914 | def testAssertSetEqual(self): |
| 915 | set1 = set() |
| 916 | set2 = set() |
| 917 | self.assertSetEqual(set1, set2) |
| 918 | |
| 919 | self.assertRaises(self.failureException, self.assertSetEqual, None, set2) |
| 920 | self.assertRaises(self.failureException, self.assertSetEqual, [], set2) |
| 921 | self.assertRaises(self.failureException, self.assertSetEqual, set1, None) |
| 922 | self.assertRaises(self.failureException, self.assertSetEqual, set1, []) |
| 923 | |
| 924 | set1 = set(['a']) |
| 925 | set2 = set() |
| 926 | self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) |
| 927 | |
| 928 | set1 = set(['a']) |
| 929 | set2 = set(['a']) |
| 930 | self.assertSetEqual(set1, set2) |
| 931 | |
| 932 | set1 = set(['a']) |
| 933 | set2 = set(['a', 'b']) |
| 934 | self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) |
| 935 | |
| 936 | set1 = set(['a']) |
| 937 | set2 = frozenset(['a', 'b']) |
| 938 | self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) |
| 939 | |
| 940 | set1 = set(['a', 'b']) |
| 941 | set2 = frozenset(['a', 'b']) |
| 942 | self.assertSetEqual(set1, set2) |
| 943 | |
| 944 | set1 = set() |
| 945 | set2 = "foo" |
| 946 | self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) |
| 947 | self.assertRaises(self.failureException, self.assertSetEqual, set2, set1) |
| 948 | |
| 949 | # make sure any string formatting is tuple-safe |
| 950 | set1 = set([(0, 1), (2, 3)]) |
| 951 | set2 = set([(4, 5)]) |
| 952 | self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) |
| 953 | |
| 954 | def testInequality(self): |
| 955 | # Try ints |
| 956 | self.assertGreater(2, 1) |
| 957 | self.assertGreaterEqual(2, 1) |
| 958 | self.assertGreaterEqual(1, 1) |
| 959 | self.assertLess(1, 2) |
| 960 | self.assertLessEqual(1, 2) |
| 961 | self.assertLessEqual(1, 1) |
| 962 | self.assertRaises(self.failureException, self.assertGreater, 1, 2) |
| 963 | self.assertRaises(self.failureException, self.assertGreater, 1, 1) |
| 964 | self.assertRaises(self.failureException, self.assertGreaterEqual, 1, 2) |
| 965 | self.assertRaises(self.failureException, self.assertLess, 2, 1) |
| 966 | self.assertRaises(self.failureException, self.assertLess, 1, 1) |
| 967 | self.assertRaises(self.failureException, self.assertLessEqual, 2, 1) |
| 968 | |
| 969 | # Try Floats |
| 970 | self.assertGreater(1.1, 1.0) |
| 971 | self.assertGreaterEqual(1.1, 1.0) |
| 972 | self.assertGreaterEqual(1.0, 1.0) |
| 973 | self.assertLess(1.0, 1.1) |
| 974 | self.assertLessEqual(1.0, 1.1) |
| 975 | self.assertLessEqual(1.0, 1.0) |
| 976 | self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.1) |
| 977 | self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.0) |
| 978 | self.assertRaises(self.failureException, self.assertGreaterEqual, 1.0, 1.1) |
| 979 | self.assertRaises(self.failureException, self.assertLess, 1.1, 1.0) |
| 980 | self.assertRaises(self.failureException, self.assertLess, 1.0, 1.0) |
| 981 | self.assertRaises(self.failureException, self.assertLessEqual, 1.1, 1.0) |
| 982 | |
| 983 | # Try Strings |
| 984 | self.assertGreater('bug', 'ant') |
| 985 | self.assertGreaterEqual('bug', 'ant') |
| 986 | self.assertGreaterEqual('ant', 'ant') |
| 987 | self.assertLess('ant', 'bug') |
| 988 | self.assertLessEqual('ant', 'bug') |
| 989 | self.assertLessEqual('ant', 'ant') |
| 990 | self.assertRaises(self.failureException, self.assertGreater, 'ant', 'bug') |
| 991 | self.assertRaises(self.failureException, self.assertGreater, 'ant', 'ant') |
| 992 | self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 'bug') |
| 993 | self.assertRaises(self.failureException, self.assertLess, 'bug', 'ant') |
| 994 | self.assertRaises(self.failureException, self.assertLess, 'ant', 'ant') |
| 995 | self.assertRaises(self.failureException, self.assertLessEqual, 'bug', 'ant') |
| 996 | |
| 997 | # Try bytes |
| 998 | self.assertGreater(b'bug', b'ant') |
| 999 | self.assertGreaterEqual(b'bug', b'ant') |
| 1000 | self.assertGreaterEqual(b'ant', b'ant') |
| 1001 | self.assertLess(b'ant', b'bug') |
| 1002 | self.assertLessEqual(b'ant', b'bug') |
| 1003 | self.assertLessEqual(b'ant', b'ant') |
| 1004 | self.assertRaises(self.failureException, self.assertGreater, b'ant', b'bug') |
| 1005 | self.assertRaises(self.failureException, self.assertGreater, b'ant', b'ant') |
| 1006 | self.assertRaises(self.failureException, self.assertGreaterEqual, b'ant', |
| 1007 | b'bug') |
| 1008 | self.assertRaises(self.failureException, self.assertLess, b'bug', b'ant') |
| 1009 | self.assertRaises(self.failureException, self.assertLess, b'ant', b'ant') |
| 1010 | self.assertRaises(self.failureException, self.assertLessEqual, b'bug', b'ant') |
| 1011 | |
| 1012 | def testAssertMultiLineEqual(self): |
| 1013 | sample_text = """\ |
| 1014 | http://www.python.org/doc/2.3/lib/module-unittest.html |
| 1015 | test case |
| 1016 | A test case is the smallest unit of testing. [...] |
| 1017 | """ |
| 1018 | revised_sample_text = """\ |
| 1019 | http://www.python.org/doc/2.4.1/lib/module-unittest.html |
| 1020 | test case |
| 1021 | A test case is the smallest unit of testing. [...] You may provide your |
| 1022 | own implementation that does not subclass from TestCase, of course. |
| 1023 | """ |
Michael Foord | ac76074 | 2010-06-05 13:57:23 +0000 | [diff] [blame] | 1024 | sample_text_error = """\ |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1025 | - http://www.python.org/doc/2.3/lib/module-unittest.html |
| 1026 | ? ^ |
| 1027 | + http://www.python.org/doc/2.4.1/lib/module-unittest.html |
| 1028 | ? ^^^ |
| 1029 | test case |
| 1030 | - A test case is the smallest unit of testing. [...] |
| 1031 | + A test case is the smallest unit of testing. [...] You may provide your |
| 1032 | ? +++++++++++++++++++++ |
| 1033 | + own implementation that does not subclass from TestCase, of course. |
| 1034 | """ |
Michael Foord | ac76074 | 2010-06-05 13:57:23 +0000 | [diff] [blame] | 1035 | self.maxDiff = None |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1036 | try: |
| 1037 | self.assertMultiLineEqual(sample_text, revised_sample_text) |
| 1038 | except self.failureException as e: |
Michael Foord | ac76074 | 2010-06-05 13:57:23 +0000 | [diff] [blame] | 1039 | # need to remove the first line of the error message |
| 1040 | error = str(e).split('\n', 1)[1] |
| 1041 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1042 | # no fair testing ourself with ourself, and assertEqual is used for strings |
| 1043 | # so can't use assertEqual either. Just use assertTrue. |
Michael Foord | ac76074 | 2010-06-05 13:57:23 +0000 | [diff] [blame] | 1044 | self.assertTrue(sample_text_error == error) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1045 | |
Michael Foord | c653ce3 | 2010-07-10 13:52:22 +0000 | [diff] [blame] | 1046 | def testAsertEqualSingleLine(self): |
| 1047 | sample_text = "laden swallows fly slowly" |
| 1048 | revised_sample_text = "unladen swallows fly quickly" |
| 1049 | sample_text_error = """\ |
| 1050 | - laden swallows fly slowly |
| 1051 | ? ^^^^ |
| 1052 | + unladen swallows fly quickly |
| 1053 | ? ++ ^^^^^ |
| 1054 | """ |
| 1055 | try: |
| 1056 | self.assertEqual(sample_text, revised_sample_text) |
| 1057 | except self.failureException as e: |
| 1058 | error = str(e).split('\n', 1)[1] |
| 1059 | self.assertTrue(sample_text_error == error) |
| 1060 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1061 | def testAssertIsNone(self): |
| 1062 | self.assertIsNone(None) |
| 1063 | self.assertRaises(self.failureException, self.assertIsNone, False) |
| 1064 | self.assertIsNotNone('DjZoPloGears on Rails') |
| 1065 | self.assertRaises(self.failureException, self.assertIsNotNone, None) |
| 1066 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1067 | def testAssertRegex(self): |
| 1068 | self.assertRegex('asdfabasdf', r'ab+') |
| 1069 | self.assertRaises(self.failureException, self.assertRegex, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1070 | 'saaas', r'aaaa') |
| 1071 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1072 | def testAssertRaisesRegex(self): |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1073 | class ExceptionMock(Exception): |
| 1074 | pass |
| 1075 | |
| 1076 | def Stub(): |
| 1077 | raise ExceptionMock('We expect') |
| 1078 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1079 | self.assertRaisesRegex(ExceptionMock, re.compile('expect$'), Stub) |
| 1080 | self.assertRaisesRegex(ExceptionMock, 'expect$', Stub) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1081 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1082 | def testAssertNotRaisesRegex(self): |
| 1083 | self.assertRaisesRegex( |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1084 | self.failureException, '^Exception not raised by <lambda>$', |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1085 | self.assertRaisesRegex, Exception, re.compile('x'), |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1086 | lambda: None) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1087 | self.assertRaisesRegex( |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1088 | self.failureException, '^Exception not raised by <lambda>$', |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1089 | self.assertRaisesRegex, Exception, 'x', |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1090 | lambda: None) |
| 1091 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1092 | def testAssertRaisesRegexMismatch(self): |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1093 | def Stub(): |
| 1094 | raise Exception('Unexpected') |
| 1095 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1096 | self.assertRaisesRegex( |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1097 | self.failureException, |
| 1098 | r'"\^Expected\$" does not match "Unexpected"', |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1099 | self.assertRaisesRegex, Exception, '^Expected$', |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1100 | Stub) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1101 | self.assertRaisesRegex( |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1102 | self.failureException, |
| 1103 | r'"\^Expected\$" does not match "Unexpected"', |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1104 | self.assertRaisesRegex, Exception, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1105 | re.compile('^Expected$'), Stub) |
| 1106 | |
| 1107 | def testAssertRaisesExcValue(self): |
| 1108 | class ExceptionMock(Exception): |
| 1109 | pass |
| 1110 | |
| 1111 | def Stub(foo): |
| 1112 | raise ExceptionMock(foo) |
| 1113 | v = "particular value" |
| 1114 | |
| 1115 | ctx = self.assertRaises(ExceptionMock) |
| 1116 | with ctx: |
| 1117 | Stub(v) |
| 1118 | e = ctx.exception |
| 1119 | self.assertIsInstance(e, ExceptionMock) |
| 1120 | self.assertEqual(e.args[0], v) |
| 1121 | |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1122 | def testAssertWarnsCallable(self): |
| 1123 | def _runtime_warn(): |
| 1124 | warnings.warn("foo", RuntimeWarning) |
| 1125 | # Success when the right warning is triggered, even several times |
| 1126 | self.assertWarns(RuntimeWarning, _runtime_warn) |
| 1127 | self.assertWarns(RuntimeWarning, _runtime_warn) |
| 1128 | # A tuple of warning classes is accepted |
| 1129 | self.assertWarns((DeprecationWarning, RuntimeWarning), _runtime_warn) |
| 1130 | # *args and **kwargs also work |
| 1131 | self.assertWarns(RuntimeWarning, |
| 1132 | warnings.warn, "foo", category=RuntimeWarning) |
| 1133 | # Failure when no warning is triggered |
| 1134 | with self.assertRaises(self.failureException): |
| 1135 | self.assertWarns(RuntimeWarning, lambda: 0) |
| 1136 | # Failure when another warning is triggered |
| 1137 | with warnings.catch_warnings(): |
| 1138 | # Force default filter (in case tests are run with -We) |
| 1139 | warnings.simplefilter("default", RuntimeWarning) |
| 1140 | with self.assertRaises(self.failureException): |
| 1141 | self.assertWarns(DeprecationWarning, _runtime_warn) |
| 1142 | # Filters for other warnings are not modified |
| 1143 | with warnings.catch_warnings(): |
| 1144 | warnings.simplefilter("error", RuntimeWarning) |
| 1145 | with self.assertRaises(RuntimeWarning): |
| 1146 | self.assertWarns(DeprecationWarning, _runtime_warn) |
| 1147 | |
| 1148 | def testAssertWarnsContext(self): |
| 1149 | # Believe it or not, it is preferrable to duplicate all tests above, |
| 1150 | # to make sure the __warningregistry__ $@ is circumvented correctly. |
| 1151 | def _runtime_warn(): |
| 1152 | warnings.warn("foo", RuntimeWarning) |
| 1153 | _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1] |
| 1154 | with self.assertWarns(RuntimeWarning) as cm: |
| 1155 | _runtime_warn() |
| 1156 | # A tuple of warning classes is accepted |
| 1157 | with self.assertWarns((DeprecationWarning, RuntimeWarning)) as cm: |
| 1158 | _runtime_warn() |
| 1159 | # The context manager exposes various useful attributes |
| 1160 | self.assertIsInstance(cm.warning, RuntimeWarning) |
| 1161 | self.assertEqual(cm.warning.args[0], "foo") |
| 1162 | self.assertIn("test_case.py", cm.filename) |
| 1163 | self.assertEqual(cm.lineno, _runtime_warn_lineno + 1) |
| 1164 | # Same with several warnings |
| 1165 | with self.assertWarns(RuntimeWarning): |
| 1166 | _runtime_warn() |
| 1167 | _runtime_warn() |
| 1168 | with self.assertWarns(RuntimeWarning): |
| 1169 | warnings.warn("foo", category=RuntimeWarning) |
| 1170 | # Failure when no warning is triggered |
| 1171 | with self.assertRaises(self.failureException): |
| 1172 | with self.assertWarns(RuntimeWarning): |
| 1173 | pass |
| 1174 | # Failure when another warning is triggered |
| 1175 | with warnings.catch_warnings(): |
| 1176 | # Force default filter (in case tests are run with -We) |
| 1177 | warnings.simplefilter("default", RuntimeWarning) |
| 1178 | with self.assertRaises(self.failureException): |
| 1179 | with self.assertWarns(DeprecationWarning): |
| 1180 | _runtime_warn() |
| 1181 | # Filters for other warnings are not modified |
| 1182 | with warnings.catch_warnings(): |
| 1183 | warnings.simplefilter("error", RuntimeWarning) |
| 1184 | with self.assertRaises(RuntimeWarning): |
| 1185 | with self.assertWarns(DeprecationWarning): |
| 1186 | _runtime_warn() |
| 1187 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1188 | def testAssertWarnsRegexCallable(self): |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1189 | def _runtime_warn(msg): |
| 1190 | warnings.warn(msg, RuntimeWarning) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1191 | self.assertWarnsRegex(RuntimeWarning, "o+", |
| 1192 | _runtime_warn, "foox") |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1193 | # Failure when no warning is triggered |
| 1194 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1195 | self.assertWarnsRegex(RuntimeWarning, "o+", |
| 1196 | lambda: 0) |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1197 | # Failure when another warning is triggered |
| 1198 | with warnings.catch_warnings(): |
| 1199 | # Force default filter (in case tests are run with -We) |
| 1200 | warnings.simplefilter("default", RuntimeWarning) |
| 1201 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1202 | self.assertWarnsRegex(DeprecationWarning, "o+", |
| 1203 | _runtime_warn, "foox") |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1204 | # Failure when message doesn't match |
| 1205 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1206 | self.assertWarnsRegex(RuntimeWarning, "o+", |
| 1207 | _runtime_warn, "barz") |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1208 | # A little trickier: we ask RuntimeWarnings to be raised, and then |
| 1209 | # check for some of them. It is implementation-defined whether |
| 1210 | # non-matching RuntimeWarnings are simply re-raised, or produce a |
| 1211 | # failureException. |
| 1212 | with warnings.catch_warnings(): |
| 1213 | warnings.simplefilter("error", RuntimeWarning) |
| 1214 | with self.assertRaises((RuntimeWarning, self.failureException)): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1215 | self.assertWarnsRegex(RuntimeWarning, "o+", |
| 1216 | _runtime_warn, "barz") |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1217 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1218 | def testAssertWarnsRegexContext(self): |
| 1219 | # Same as above, but with assertWarnsRegex as a context manager |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1220 | def _runtime_warn(msg): |
| 1221 | warnings.warn(msg, RuntimeWarning) |
| 1222 | _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1] |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1223 | with self.assertWarnsRegex(RuntimeWarning, "o+") as cm: |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1224 | _runtime_warn("foox") |
| 1225 | self.assertIsInstance(cm.warning, RuntimeWarning) |
| 1226 | self.assertEqual(cm.warning.args[0], "foox") |
| 1227 | self.assertIn("test_case.py", cm.filename) |
| 1228 | self.assertEqual(cm.lineno, _runtime_warn_lineno + 1) |
| 1229 | # Failure when no warning is triggered |
| 1230 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1231 | with self.assertWarnsRegex(RuntimeWarning, "o+"): |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1232 | pass |
| 1233 | # Failure when another warning is triggered |
| 1234 | with warnings.catch_warnings(): |
| 1235 | # Force default filter (in case tests are run with -We) |
| 1236 | warnings.simplefilter("default", RuntimeWarning) |
| 1237 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1238 | with self.assertWarnsRegex(DeprecationWarning, "o+"): |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1239 | _runtime_warn("foox") |
| 1240 | # Failure when message doesn't match |
| 1241 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1242 | with self.assertWarnsRegex(RuntimeWarning, "o+"): |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1243 | _runtime_warn("barz") |
| 1244 | # A little trickier: we ask RuntimeWarnings to be raised, and then |
| 1245 | # check for some of them. It is implementation-defined whether |
| 1246 | # non-matching RuntimeWarnings are simply re-raised, or produce a |
| 1247 | # failureException. |
| 1248 | with warnings.catch_warnings(): |
| 1249 | warnings.simplefilter("error", RuntimeWarning) |
| 1250 | with self.assertRaises((RuntimeWarning, self.failureException)): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1251 | with self.assertWarnsRegex(RuntimeWarning, "o+"): |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1252 | _runtime_warn("barz") |
| 1253 | |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1254 | def testDeprecatedMethodNames(self): |
Ezio Melotti | 361467e | 2011-04-03 17:37:58 +0300 | [diff] [blame] | 1255 | """ |
| 1256 | Test that the deprecated methods raise a DeprecationWarning. See #9424. |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1257 | """ |
| 1258 | old = ( |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1259 | (self.failIfEqual, (3, 5)), |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1260 | (self.assertNotEquals, (3, 5)), |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1261 | (self.failUnlessEqual, (3, 3)), |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1262 | (self.assertEquals, (3, 3)), |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1263 | (self.failUnlessAlmostEqual, (2.0, 2.0)), |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1264 | (self.assertAlmostEquals, (2.0, 2.0)), |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1265 | (self.failIfAlmostEqual, (3.0, 5.0)), |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1266 | (self.assertNotAlmostEquals, (3.0, 5.0)), |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1267 | (self.failUnless, (True,)), |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1268 | (self.assert_, (True,)), |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1269 | (self.failUnlessRaises, (TypeError, lambda _: 3.14 + 'spam')), |
| 1270 | (self.failIf, (False,)), |
| 1271 | (self.assertDictContainsSubset, (dict(a=1, b=2), dict(a=1, b=2, c=3))), |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1272 | (self.assertRaisesRegexp, (KeyError, 'foo', lambda: {}['foo'])), |
| 1273 | (self.assertRegexpMatches, ('bar', 'bar')), |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1274 | ) |
| 1275 | for meth, args in old: |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1276 | with self.assertWarns(DeprecationWarning): |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1277 | meth(*args) |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1278 | |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1279 | # disable this test for now. When the version where the fail* methods will |
| 1280 | # be removed is decided, re-enable it and update the version |
| 1281 | def _testDeprecatedFailMethods(self): |
| 1282 | """Test that the deprecated fail* methods get removed in 3.x""" |
| 1283 | if sys.version_info[:2] < (3, 3): |
| 1284 | return |
| 1285 | deprecated_names = [ |
| 1286 | 'failIfEqual', 'failUnlessEqual', 'failUnlessAlmostEqual', |
| 1287 | 'failIfAlmostEqual', 'failUnless', 'failUnlessRaises', 'failIf', |
| 1288 | 'assertDictContainsSubset', |
| 1289 | ] |
| 1290 | for deprecated_name in deprecated_names: |
| 1291 | with self.assertRaises(AttributeError): |
| 1292 | getattr(self, deprecated_name) # remove these in 3.x |
| 1293 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1294 | def testDeepcopy(self): |
| 1295 | # Issue: 5660 |
| 1296 | class TestableTest(unittest.TestCase): |
| 1297 | def testNothing(self): |
| 1298 | pass |
| 1299 | |
| 1300 | test = TestableTest('testNothing') |
| 1301 | |
| 1302 | # This shouldn't blow up |
| 1303 | deepcopy(test) |
Michael Foord | 8ca6d98 | 2010-11-20 15:34:26 +0000 | [diff] [blame] | 1304 | |
| 1305 | def testPickle(self): |
| 1306 | # Issue 10326 |
| 1307 | |
| 1308 | # Can't use TestCase classes defined in Test class as |
| 1309 | # pickle does not work with inner classes |
| 1310 | test = unittest.TestCase('run') |
| 1311 | for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1312 | |
| 1313 | # blew up prior to fix |
| 1314 | pickled_test = pickle.dumps(test, protocol=protocol) |
Michael Foord | 8a00eec | 2010-11-20 15:43:02 +0000 | [diff] [blame] | 1315 | unpickled_test = pickle.loads(pickled_test) |
| 1316 | self.assertEqual(test, unpickled_test) |
Michael Foord | b357fb7 | 2010-11-20 15:47:56 +0000 | [diff] [blame] | 1317 | |
| 1318 | # exercise the TestCase instance in a way that will invoke |
| 1319 | # the type equality lookup mechanism |
| 1320 | unpickled_test.assertEqual(set(), set()) |
Michael Foord | b3468f7 | 2010-12-19 03:19:47 +0000 | [diff] [blame] | 1321 | |
| 1322 | def testKeyboardInterrupt(self): |
| 1323 | def _raise(self=None): |
| 1324 | raise KeyboardInterrupt |
| 1325 | def nothing(self): |
| 1326 | pass |
| 1327 | |
| 1328 | class Test1(unittest.TestCase): |
| 1329 | test_something = _raise |
| 1330 | |
| 1331 | class Test2(unittest.TestCase): |
| 1332 | setUp = _raise |
| 1333 | test_something = nothing |
| 1334 | |
| 1335 | class Test3(unittest.TestCase): |
| 1336 | test_something = nothing |
| 1337 | tearDown = _raise |
| 1338 | |
| 1339 | class Test4(unittest.TestCase): |
| 1340 | def test_something(self): |
| 1341 | self.addCleanup(_raise) |
| 1342 | |
| 1343 | for klass in (Test1, Test2, Test3, Test4): |
| 1344 | with self.assertRaises(KeyboardInterrupt): |
| 1345 | klass('test_something').run() |
| 1346 | |
| 1347 | def testSkippingEverywhere(self): |
| 1348 | def _skip(self=None): |
| 1349 | raise unittest.SkipTest('some reason') |
| 1350 | def nothing(self): |
| 1351 | pass |
| 1352 | |
| 1353 | class Test1(unittest.TestCase): |
| 1354 | test_something = _skip |
| 1355 | |
| 1356 | class Test2(unittest.TestCase): |
| 1357 | setUp = _skip |
| 1358 | test_something = nothing |
| 1359 | |
| 1360 | class Test3(unittest.TestCase): |
| 1361 | test_something = nothing |
| 1362 | tearDown = _skip |
| 1363 | |
| 1364 | class Test4(unittest.TestCase): |
| 1365 | def test_something(self): |
| 1366 | self.addCleanup(_skip) |
| 1367 | |
| 1368 | for klass in (Test1, Test2, Test3, Test4): |
| 1369 | result = unittest.TestResult() |
| 1370 | klass('test_something').run(result) |
| 1371 | self.assertEqual(len(result.skipped), 1) |
| 1372 | self.assertEqual(result.testsRun, 1) |
| 1373 | |
| 1374 | def testSystemExit(self): |
| 1375 | def _raise(self=None): |
| 1376 | raise SystemExit |
| 1377 | def nothing(self): |
| 1378 | pass |
| 1379 | |
| 1380 | class Test1(unittest.TestCase): |
| 1381 | test_something = _raise |
| 1382 | |
| 1383 | class Test2(unittest.TestCase): |
| 1384 | setUp = _raise |
| 1385 | test_something = nothing |
| 1386 | |
| 1387 | class Test3(unittest.TestCase): |
| 1388 | test_something = nothing |
| 1389 | tearDown = _raise |
| 1390 | |
| 1391 | class Test4(unittest.TestCase): |
| 1392 | def test_something(self): |
| 1393 | self.addCleanup(_raise) |
| 1394 | |
| 1395 | for klass in (Test1, Test2, Test3, Test4): |
| 1396 | result = unittest.TestResult() |
| 1397 | klass('test_something').run(result) |
| 1398 | self.assertEqual(len(result.errors), 1) |
| 1399 | self.assertEqual(result.testsRun, 1) |
Benjamin Peterson | b6ffa79 | 2011-07-14 12:48:25 -0500 | [diff] [blame] | 1400 | |
| 1401 | @support.cpython_only |
| 1402 | def testNoCycles(self): |
| 1403 | case = unittest.TestCase() |
| 1404 | wr = weakref.ref(case) |
| 1405 | with support.disable_gc(): |
| 1406 | del case |
| 1407 | self.assertFalse(wr()) |