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 ( |
| 16 | TestEquality, TestHashing, LoggingResult, |
| 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 | |
| 300 | # "This class attribute gives the exception raised by the test() method. |
| 301 | # If a test framework needs to use a specialized exception, possibly to |
| 302 | # carry additional information, it must subclass this exception in |
| 303 | # order to ``play fair'' with the framework. The initial value of this |
| 304 | # attribute is AssertionError" |
| 305 | def test_failureException__default(self): |
| 306 | class Foo(unittest.TestCase): |
| 307 | def test(self): |
| 308 | pass |
| 309 | |
| 310 | self.assertTrue(Foo('test').failureException is AssertionError) |
| 311 | |
| 312 | # "This class attribute gives the exception raised by the test() method. |
| 313 | # If a test framework needs to use a specialized exception, possibly to |
| 314 | # carry additional information, it must subclass this exception in |
| 315 | # order to ``play fair'' with the framework." |
| 316 | # |
| 317 | # Make sure TestCase.run() respects the designated failureException |
| 318 | def test_failureException__subclassing__explicit_raise(self): |
| 319 | events = [] |
| 320 | result = LoggingResult(events) |
| 321 | |
| 322 | class Foo(unittest.TestCase): |
| 323 | def test(self): |
| 324 | raise RuntimeError() |
| 325 | |
| 326 | failureException = RuntimeError |
| 327 | |
| 328 | self.assertTrue(Foo('test').failureException is RuntimeError) |
| 329 | |
| 330 | |
| 331 | Foo('test').run(result) |
| 332 | expected = ['startTest', 'addFailure', 'stopTest'] |
| 333 | self.assertEqual(events, expected) |
| 334 | |
| 335 | # "This class attribute gives the exception raised by the test() method. |
| 336 | # If a test framework needs to use a specialized exception, possibly to |
| 337 | # carry additional information, it must subclass this exception in |
| 338 | # order to ``play fair'' with the framework." |
| 339 | # |
| 340 | # Make sure TestCase.run() respects the designated failureException |
| 341 | def test_failureException__subclassing__implicit_raise(self): |
| 342 | events = [] |
| 343 | result = LoggingResult(events) |
| 344 | |
| 345 | class Foo(unittest.TestCase): |
| 346 | def test(self): |
| 347 | self.fail("foo") |
| 348 | |
| 349 | failureException = RuntimeError |
| 350 | |
| 351 | self.assertTrue(Foo('test').failureException is RuntimeError) |
| 352 | |
| 353 | |
| 354 | Foo('test').run(result) |
| 355 | expected = ['startTest', 'addFailure', 'stopTest'] |
| 356 | self.assertEqual(events, expected) |
| 357 | |
| 358 | # "The default implementation does nothing." |
| 359 | def test_setUp(self): |
| 360 | class Foo(unittest.TestCase): |
| 361 | def runTest(self): |
| 362 | pass |
| 363 | |
| 364 | # ... and nothing should happen |
| 365 | Foo().setUp() |
| 366 | |
| 367 | # "The default implementation does nothing." |
| 368 | def test_tearDown(self): |
| 369 | class Foo(unittest.TestCase): |
| 370 | def runTest(self): |
| 371 | pass |
| 372 | |
| 373 | # ... and nothing should happen |
| 374 | Foo().tearDown() |
| 375 | |
| 376 | # "Return a string identifying the specific test case." |
| 377 | # |
| 378 | # Because of the vague nature of the docs, I'm not going to lock this |
| 379 | # test down too much. Really all that can be asserted is that the id() |
| 380 | # will be a string (either 8-byte or unicode -- again, because the docs |
| 381 | # just say "string") |
| 382 | def test_id(self): |
| 383 | class Foo(unittest.TestCase): |
| 384 | def runTest(self): |
| 385 | pass |
| 386 | |
| 387 | self.assertIsInstance(Foo().id(), str) |
| 388 | |
| 389 | |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 390 | # "If result is omitted or None, a temporary result object is created, |
| 391 | # used, and is made available to the caller. As TestCase owns the |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 392 | # temporary result startTestRun and stopTestRun are called. |
| 393 | |
| 394 | def test_run__uses_defaultTestResult(self): |
| 395 | events = [] |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 396 | defaultResult = LoggingResult(events) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 397 | |
| 398 | class Foo(unittest.TestCase): |
| 399 | def test(self): |
| 400 | events.append('test') |
| 401 | |
| 402 | def defaultTestResult(self): |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 403 | return defaultResult |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 404 | |
| 405 | # Make run() find a result object on its own |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 406 | result = Foo('test').run() |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 407 | |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 408 | self.assertIs(result, defaultResult) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 409 | expected = ['startTestRun', 'startTest', 'test', 'addSuccess', |
| 410 | 'stopTest', 'stopTestRun'] |
| 411 | self.assertEqual(events, expected) |
| 412 | |
Michael Foord | 1341bb0 | 2011-03-14 19:01:46 -0400 | [diff] [blame] | 413 | |
| 414 | # "The result object is returned to run's caller" |
| 415 | def test_run__returns_given_result(self): |
| 416 | |
| 417 | class Foo(unittest.TestCase): |
| 418 | def test(self): |
| 419 | pass |
| 420 | |
| 421 | result = unittest.TestResult() |
| 422 | |
| 423 | retval = Foo('test').run(result) |
| 424 | self.assertIs(retval, result) |
| 425 | |
| 426 | |
| 427 | # "The same effect [as method run] may be had by simply calling the |
| 428 | # TestCase instance." |
| 429 | def test_call__invoking_an_instance_delegates_to_run(self): |
| 430 | resultIn = unittest.TestResult() |
| 431 | resultOut = unittest.TestResult() |
| 432 | |
| 433 | class Foo(unittest.TestCase): |
| 434 | def test(self): |
| 435 | pass |
| 436 | |
| 437 | def run(self, result): |
| 438 | self.assertIs(result, resultIn) |
| 439 | return resultOut |
| 440 | |
| 441 | retval = Foo('test')(resultIn) |
| 442 | |
| 443 | self.assertIs(retval, resultOut) |
| 444 | |
| 445 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 446 | def testShortDescriptionWithoutDocstring(self): |
| 447 | self.assertIsNone(self.shortDescription()) |
| 448 | |
| 449 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 450 | "Docstrings are omitted with -O2 and above") |
| 451 | def testShortDescriptionWithOneLineDocstring(self): |
| 452 | """Tests shortDescription() for a method with a docstring.""" |
| 453 | self.assertEqual( |
| 454 | self.shortDescription(), |
| 455 | 'Tests shortDescription() for a method with a docstring.') |
| 456 | |
| 457 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 458 | "Docstrings are omitted with -O2 and above") |
| 459 | def testShortDescriptionWithMultiLineDocstring(self): |
| 460 | """Tests shortDescription() for a method with a longer docstring. |
| 461 | |
| 462 | This method ensures that only the first line of a docstring is |
| 463 | returned used in the short description, no matter how long the |
| 464 | whole thing is. |
| 465 | """ |
| 466 | self.assertEqual( |
| 467 | self.shortDescription(), |
| 468 | 'Tests shortDescription() for a method with a longer ' |
| 469 | 'docstring.') |
| 470 | |
| 471 | def testAddTypeEqualityFunc(self): |
| 472 | class SadSnake(object): |
| 473 | """Dummy class for test_addTypeEqualityFunc.""" |
| 474 | s1, s2 = SadSnake(), SadSnake() |
| 475 | self.assertFalse(s1 == s2) |
| 476 | def AllSnakesCreatedEqual(a, b, msg=None): |
| 477 | return type(a) == type(b) == SadSnake |
| 478 | self.addTypeEqualityFunc(SadSnake, AllSnakesCreatedEqual) |
| 479 | self.assertEqual(s1, s2) |
| 480 | # No this doesn't clean up and remove the SadSnake equality func |
| 481 | # from this TestCase instance but since its a local nothing else |
| 482 | # will ever notice that. |
| 483 | |
| 484 | def testAssertIs(self): |
| 485 | thing = object() |
| 486 | self.assertIs(thing, thing) |
| 487 | self.assertRaises(self.failureException, self.assertIs, thing, object()) |
| 488 | |
| 489 | def testAssertIsNot(self): |
| 490 | thing = object() |
| 491 | self.assertIsNot(thing, object()) |
| 492 | self.assertRaises(self.failureException, self.assertIsNot, thing, thing) |
| 493 | |
| 494 | def testAssertIsInstance(self): |
| 495 | thing = [] |
| 496 | self.assertIsInstance(thing, list) |
| 497 | self.assertRaises(self.failureException, self.assertIsInstance, |
| 498 | thing, dict) |
| 499 | |
| 500 | def testAssertNotIsInstance(self): |
| 501 | thing = [] |
| 502 | self.assertNotIsInstance(thing, dict) |
| 503 | self.assertRaises(self.failureException, self.assertNotIsInstance, |
| 504 | thing, list) |
| 505 | |
| 506 | def testAssertIn(self): |
| 507 | animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'} |
| 508 | |
| 509 | self.assertIn('a', 'abc') |
| 510 | self.assertIn(2, [1, 2, 3]) |
| 511 | self.assertIn('monkey', animals) |
| 512 | |
| 513 | self.assertNotIn('d', 'abc') |
| 514 | self.assertNotIn(0, [1, 2, 3]) |
| 515 | self.assertNotIn('otter', animals) |
| 516 | |
| 517 | self.assertRaises(self.failureException, self.assertIn, 'x', 'abc') |
| 518 | self.assertRaises(self.failureException, self.assertIn, 4, [1, 2, 3]) |
| 519 | self.assertRaises(self.failureException, self.assertIn, 'elephant', |
| 520 | animals) |
| 521 | |
| 522 | self.assertRaises(self.failureException, self.assertNotIn, 'c', 'abc') |
| 523 | self.assertRaises(self.failureException, self.assertNotIn, 1, [1, 2, 3]) |
| 524 | self.assertRaises(self.failureException, self.assertNotIn, 'cow', |
| 525 | animals) |
| 526 | |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 527 | def testAssertDictContainsSubset(self): |
| 528 | with warnings.catch_warnings(): |
| 529 | warnings.simplefilter("ignore", DeprecationWarning) |
| 530 | |
| 531 | self.assertDictContainsSubset({}, {}) |
| 532 | self.assertDictContainsSubset({}, {'a': 1}) |
| 533 | self.assertDictContainsSubset({'a': 1}, {'a': 1}) |
| 534 | self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2}) |
| 535 | self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2}) |
| 536 | |
| 537 | with self.assertRaises(self.failureException): |
| 538 | self.assertDictContainsSubset({1: "one"}, {}) |
| 539 | |
| 540 | with self.assertRaises(self.failureException): |
| 541 | self.assertDictContainsSubset({'a': 2}, {'a': 1}) |
| 542 | |
| 543 | with self.assertRaises(self.failureException): |
| 544 | self.assertDictContainsSubset({'c': 1}, {'a': 1}) |
| 545 | |
| 546 | with self.assertRaises(self.failureException): |
| 547 | self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1}) |
| 548 | |
| 549 | with self.assertRaises(self.failureException): |
| 550 | self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1}) |
| 551 | |
| 552 | one = ''.join(chr(i) for i in range(255)) |
| 553 | # this used to cause a UnicodeDecodeError constructing the failure msg |
| 554 | with self.assertRaises(self.failureException): |
| 555 | self.assertDictContainsSubset({'foo': one}, {'foo': '\uFFFD'}) |
| 556 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 557 | def testAssertEqual(self): |
| 558 | equal_pairs = [ |
| 559 | ((), ()), |
| 560 | ({}, {}), |
| 561 | ([], []), |
| 562 | (set(), set()), |
| 563 | (frozenset(), frozenset())] |
| 564 | for a, b in equal_pairs: |
| 565 | # This mess of try excepts is to test the assertEqual behavior |
| 566 | # itself. |
| 567 | try: |
| 568 | self.assertEqual(a, b) |
| 569 | except self.failureException: |
| 570 | self.fail('assertEqual(%r, %r) failed' % (a, b)) |
| 571 | try: |
| 572 | self.assertEqual(a, b, msg='foo') |
| 573 | except self.failureException: |
| 574 | self.fail('assertEqual(%r, %r) with msg= failed' % (a, b)) |
| 575 | try: |
| 576 | self.assertEqual(a, b, 'foo') |
| 577 | except self.failureException: |
| 578 | self.fail('assertEqual(%r, %r) with third parameter failed' % |
| 579 | (a, b)) |
| 580 | |
| 581 | unequal_pairs = [ |
| 582 | ((), []), |
| 583 | ({}, set()), |
| 584 | (set([4,1]), frozenset([4,2])), |
| 585 | (frozenset([4,5]), set([2,3])), |
| 586 | (set([3,4]), set([5,4]))] |
| 587 | for a, b in unequal_pairs: |
| 588 | self.assertRaises(self.failureException, self.assertEqual, a, b) |
| 589 | self.assertRaises(self.failureException, self.assertEqual, a, b, |
| 590 | 'foo') |
| 591 | self.assertRaises(self.failureException, self.assertEqual, a, b, |
| 592 | msg='foo') |
| 593 | |
| 594 | def testEquality(self): |
| 595 | self.assertListEqual([], []) |
| 596 | self.assertTupleEqual((), ()) |
| 597 | self.assertSequenceEqual([], ()) |
| 598 | |
| 599 | a = [0, 'a', []] |
| 600 | b = [] |
| 601 | self.assertRaises(unittest.TestCase.failureException, |
| 602 | self.assertListEqual, a, b) |
| 603 | self.assertRaises(unittest.TestCase.failureException, |
| 604 | self.assertListEqual, tuple(a), tuple(b)) |
| 605 | self.assertRaises(unittest.TestCase.failureException, |
| 606 | self.assertSequenceEqual, a, tuple(b)) |
| 607 | |
| 608 | b.extend(a) |
| 609 | self.assertListEqual(a, b) |
| 610 | self.assertTupleEqual(tuple(a), tuple(b)) |
| 611 | self.assertSequenceEqual(a, tuple(b)) |
| 612 | self.assertSequenceEqual(tuple(a), b) |
| 613 | |
| 614 | self.assertRaises(self.failureException, self.assertListEqual, |
| 615 | a, tuple(b)) |
| 616 | self.assertRaises(self.failureException, self.assertTupleEqual, |
| 617 | tuple(a), b) |
| 618 | self.assertRaises(self.failureException, self.assertListEqual, None, b) |
| 619 | self.assertRaises(self.failureException, self.assertTupleEqual, None, |
| 620 | tuple(b)) |
| 621 | self.assertRaises(self.failureException, self.assertSequenceEqual, |
| 622 | None, tuple(b)) |
| 623 | self.assertRaises(self.failureException, self.assertListEqual, 1, 1) |
| 624 | self.assertRaises(self.failureException, self.assertTupleEqual, 1, 1) |
| 625 | self.assertRaises(self.failureException, self.assertSequenceEqual, |
| 626 | 1, 1) |
| 627 | |
| 628 | self.assertDictEqual({}, {}) |
| 629 | |
| 630 | c = { 'x': 1 } |
| 631 | d = {} |
| 632 | self.assertRaises(unittest.TestCase.failureException, |
| 633 | self.assertDictEqual, c, d) |
| 634 | |
| 635 | d.update(c) |
| 636 | self.assertDictEqual(c, d) |
| 637 | |
| 638 | d['x'] = 0 |
| 639 | self.assertRaises(unittest.TestCase.failureException, |
| 640 | self.assertDictEqual, c, d, 'These are unequal') |
| 641 | |
| 642 | self.assertRaises(self.failureException, self.assertDictEqual, None, d) |
| 643 | self.assertRaises(self.failureException, self.assertDictEqual, [], d) |
| 644 | self.assertRaises(self.failureException, self.assertDictEqual, 1, 1) |
| 645 | |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 646 | def testAssertSequenceEqualMaxDiff(self): |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 647 | self.assertEqual(self.maxDiff, 80*8) |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 648 | seq1 = 'a' + 'x' * 80**2 |
| 649 | seq2 = 'b' + 'x' * 80**2 |
| 650 | diff = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(), |
| 651 | pprint.pformat(seq2).splitlines())) |
Michael Foord | da562f6 | 2010-06-05 21:01:08 +0000 | [diff] [blame] | 652 | # the +1 is the leading \n added by assertSequenceEqual |
| 653 | omitted = unittest.case.DIFF_OMITTED % (len(diff) + 1,) |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 654 | |
| 655 | self.maxDiff = len(diff)//2 |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 656 | try: |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 657 | |
| 658 | self.assertSequenceEqual(seq1, seq2) |
| 659 | except self.failureException as e: |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 660 | msg = e.args[0] |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 661 | else: |
| 662 | self.fail('assertSequenceEqual did not fail.') |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 663 | self.assertTrue(len(msg) < len(diff)) |
Michael Foord | da562f6 | 2010-06-05 21:01:08 +0000 | [diff] [blame] | 664 | self.assertIn(omitted, msg) |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 665 | |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 666 | self.maxDiff = len(diff) * 2 |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 667 | try: |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 668 | self.assertSequenceEqual(seq1, seq2) |
| 669 | except self.failureException as e: |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 670 | msg = e.args[0] |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 671 | else: |
| 672 | self.fail('assertSequenceEqual did not fail.') |
| 673 | self.assertTrue(len(msg) > len(diff)) |
Michael Foord | da562f6 | 2010-06-05 21:01:08 +0000 | [diff] [blame] | 674 | self.assertNotIn(omitted, msg) |
Michael Foord | 085dfd3 | 2010-06-05 12:17:02 +0000 | [diff] [blame] | 675 | |
| 676 | self.maxDiff = None |
| 677 | try: |
| 678 | self.assertSequenceEqual(seq1, seq2) |
| 679 | except self.failureException as e: |
| 680 | msg = e.args[0] |
| 681 | else: |
| 682 | self.fail('assertSequenceEqual did not fail.') |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 683 | self.assertTrue(len(msg) > len(diff)) |
Michael Foord | da562f6 | 2010-06-05 21:01:08 +0000 | [diff] [blame] | 684 | self.assertNotIn(omitted, msg) |
| 685 | |
| 686 | def testTruncateMessage(self): |
| 687 | self.maxDiff = 1 |
| 688 | message = self._truncateMessage('foo', 'bar') |
| 689 | omitted = unittest.case.DIFF_OMITTED % len('bar') |
| 690 | self.assertEqual(message, 'foo' + omitted) |
| 691 | |
| 692 | self.maxDiff = None |
| 693 | message = self._truncateMessage('foo', 'bar') |
| 694 | self.assertEqual(message, 'foobar') |
| 695 | |
Michael Foord | 520ed0a | 2010-06-05 21:12:23 +0000 | [diff] [blame] | 696 | self.maxDiff = 4 |
| 697 | message = self._truncateMessage('foo', 'bar') |
| 698 | self.assertEqual(message, 'foobar') |
| 699 | |
Michael Foord | da562f6 | 2010-06-05 21:01:08 +0000 | [diff] [blame] | 700 | def testAssertDictEqualTruncates(self): |
| 701 | test = unittest.TestCase('assertEqual') |
| 702 | def truncate(msg, diff): |
| 703 | return 'foo' |
| 704 | test._truncateMessage = truncate |
| 705 | try: |
| 706 | test.assertDictEqual({}, {1: 0}) |
| 707 | except self.failureException as e: |
| 708 | self.assertEqual(str(e), 'foo') |
| 709 | else: |
| 710 | self.fail('assertDictEqual did not fail') |
| 711 | |
| 712 | def testAssertMultiLineEqualTruncates(self): |
| 713 | test = unittest.TestCase('assertEqual') |
| 714 | def truncate(msg, diff): |
| 715 | return 'foo' |
| 716 | test._truncateMessage = truncate |
| 717 | try: |
| 718 | test.assertMultiLineEqual('foo', 'bar') |
| 719 | except self.failureException as e: |
| 720 | self.assertEqual(str(e), 'foo') |
| 721 | else: |
| 722 | self.fail('assertMultiLineEqual did not fail') |
Michael Foord | 2034d9a | 2010-06-05 11:27:52 +0000 | [diff] [blame] | 723 | |
Ezio Melotti | edd117f | 2011-04-27 10:20:38 +0300 | [diff] [blame] | 724 | def testAssertEqual_diffThreshold(self): |
| 725 | # check threshold value |
| 726 | self.assertEqual(self._diffThreshold, 2**16) |
| 727 | # disable madDiff to get diff markers |
| 728 | self.maxDiff = None |
| 729 | |
| 730 | # set a lower threshold value and add a cleanup to restore it |
| 731 | old_threshold = self._diffThreshold |
| 732 | self._diffThreshold = 2**8 |
| 733 | self.addCleanup(lambda: setattr(self, '_diffThreshold', old_threshold)) |
| 734 | |
| 735 | # under the threshold: diff marker (^) in error message |
| 736 | s = 'x' * (2**7) |
| 737 | with self.assertRaises(self.failureException) as cm: |
| 738 | self.assertEqual(s + 'a', s + 'b') |
| 739 | self.assertIn('^', str(cm.exception)) |
| 740 | self.assertEqual(s + 'a', s + 'a') |
| 741 | |
| 742 | # over the threshold: diff not used and marker (^) not in error message |
| 743 | s = 'x' * (2**9) |
| 744 | # if the path that uses difflib is taken, _truncateMessage will be |
| 745 | # called -- replace it with explodingTruncation to verify that this |
| 746 | # doesn't happen |
| 747 | def explodingTruncation(message, diff): |
| 748 | raise SystemError('this should not be raised') |
| 749 | old_truncate = self._truncateMessage |
| 750 | self._truncateMessage = explodingTruncation |
| 751 | self.addCleanup(lambda: setattr(self, '_truncateMessage', old_truncate)) |
| 752 | |
| 753 | s1, s2 = s + 'a', s + 'b' |
| 754 | with self.assertRaises(self.failureException) as cm: |
| 755 | self.assertEqual(s1, s2) |
| 756 | self.assertNotIn('^', str(cm.exception)) |
| 757 | self.assertEqual(str(cm.exception), '%r != %r' % (s1, s2)) |
| 758 | self.assertEqual(s + 'a', s + 'a') |
| 759 | |
Raymond Hettinger | f954217 | 2010-12-24 10:30:06 +0000 | [diff] [blame] | 760 | def testAssertCountEqual(self): |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 761 | a = object() |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 762 | self.assertCountEqual([1, 2, 3], [3, 2, 1]) |
| 763 | self.assertCountEqual(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo']) |
| 764 | self.assertCountEqual([a, a, 2, 2, 3], (a, 2, 3, a, 2)) |
| 765 | self.assertCountEqual([1, "2", "a", "a"], ["a", "2", True, "a"]) |
| 766 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 767 | [1, 2] + [3] * 100, [1] * 100 + [2, 3]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 768 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 769 | [1, "2", "a", "a"], ["a", "2", True, 1]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 770 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 771 | [10], [10, 11]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 772 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 773 | [10, 11], [10]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 774 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 775 | [10, 11, 10], [10, 11]) |
| 776 | |
| 777 | # Test that sequences of unhashable objects can be tested for sameness: |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 778 | self.assertCountEqual([[1, 2], [3, 4], 0], [False, [3, 4], [1, 2]]) |
Raymond Hettinger | 8396124 | 2010-12-24 00:48:47 +0000 | [diff] [blame] | 779 | # Test that iterator of unhashable objects can be tested for sameness: |
| 780 | self.assertCountEqual(iter([1, 2, [], 3, 4]), |
| 781 | iter([1, 2, [], 3, 4])) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 782 | |
| 783 | # hashable types, but not orderable |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 784 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 785 | [], [divmod, 'x', 1, 5j, 2j, frozenset()]) |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 786 | # comparing dicts |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 787 | self.assertCountEqual([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}]) |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 788 | # comparing heterogenous non-hashable sequences |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 789 | self.assertCountEqual([1, 'x', divmod, []], [divmod, [], 'x', 1]) |
| 790 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 791 | [], [divmod, [], 'x', 1, 5j, 2j, set()]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 792 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 793 | [[1]], [[2]]) |
| 794 | |
| 795 | # Same elements, but not same sequence length |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 796 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 797 | [1, 1, 2], [2, 1]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 798 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 799 | [1, 1, "2", "a", "a"], ["2", "2", True, "a"]) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 800 | self.assertRaises(self.failureException, self.assertCountEqual, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 801 | [1, {'b': 2}, None, True], [{'b': 2}, True, None]) |
| 802 | |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 803 | # Same elements which don't reliably compare, in |
| 804 | # different order, see issue 10242 |
| 805 | a = [{2,4}, {1,2}] |
| 806 | b = a[::-1] |
| 807 | self.assertCountEqual(a, b) |
| 808 | |
Raymond Hettinger | f954217 | 2010-12-24 10:30:06 +0000 | [diff] [blame] | 809 | # test utility functions supporting assertCountEqual() |
| 810 | |
| 811 | diffs = set(unittest.util._count_diff_all_purpose('aaabccd', 'abbbcce')) |
| 812 | expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')} |
| 813 | self.assertEqual(diffs, expected) |
| 814 | |
| 815 | diffs = unittest.util._count_diff_all_purpose([[]], []) |
| 816 | self.assertEqual(diffs, [(1, 0, [])]) |
| 817 | |
| 818 | diffs = set(unittest.util._count_diff_hashable('aaabccd', 'abbbcce')) |
| 819 | expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')} |
| 820 | self.assertEqual(diffs, expected) |
Raymond Hettinger | 6e165b3 | 2010-11-27 09:31:37 +0000 | [diff] [blame] | 821 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 822 | def testAssertSetEqual(self): |
| 823 | set1 = set() |
| 824 | set2 = set() |
| 825 | self.assertSetEqual(set1, set2) |
| 826 | |
| 827 | self.assertRaises(self.failureException, self.assertSetEqual, None, set2) |
| 828 | self.assertRaises(self.failureException, self.assertSetEqual, [], set2) |
| 829 | self.assertRaises(self.failureException, self.assertSetEqual, set1, None) |
| 830 | self.assertRaises(self.failureException, self.assertSetEqual, set1, []) |
| 831 | |
| 832 | set1 = set(['a']) |
| 833 | set2 = set() |
| 834 | self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) |
| 835 | |
| 836 | set1 = set(['a']) |
| 837 | set2 = set(['a']) |
| 838 | self.assertSetEqual(set1, set2) |
| 839 | |
| 840 | set1 = set(['a']) |
| 841 | set2 = set(['a', 'b']) |
| 842 | self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) |
| 843 | |
| 844 | set1 = set(['a']) |
| 845 | set2 = frozenset(['a', 'b']) |
| 846 | self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) |
| 847 | |
| 848 | set1 = set(['a', 'b']) |
| 849 | set2 = frozenset(['a', 'b']) |
| 850 | self.assertSetEqual(set1, set2) |
| 851 | |
| 852 | set1 = set() |
| 853 | set2 = "foo" |
| 854 | self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) |
| 855 | self.assertRaises(self.failureException, self.assertSetEqual, set2, set1) |
| 856 | |
| 857 | # make sure any string formatting is tuple-safe |
| 858 | set1 = set([(0, 1), (2, 3)]) |
| 859 | set2 = set([(4, 5)]) |
| 860 | self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) |
| 861 | |
| 862 | def testInequality(self): |
| 863 | # Try ints |
| 864 | self.assertGreater(2, 1) |
| 865 | self.assertGreaterEqual(2, 1) |
| 866 | self.assertGreaterEqual(1, 1) |
| 867 | self.assertLess(1, 2) |
| 868 | self.assertLessEqual(1, 2) |
| 869 | self.assertLessEqual(1, 1) |
| 870 | self.assertRaises(self.failureException, self.assertGreater, 1, 2) |
| 871 | self.assertRaises(self.failureException, self.assertGreater, 1, 1) |
| 872 | self.assertRaises(self.failureException, self.assertGreaterEqual, 1, 2) |
| 873 | self.assertRaises(self.failureException, self.assertLess, 2, 1) |
| 874 | self.assertRaises(self.failureException, self.assertLess, 1, 1) |
| 875 | self.assertRaises(self.failureException, self.assertLessEqual, 2, 1) |
| 876 | |
| 877 | # Try Floats |
| 878 | self.assertGreater(1.1, 1.0) |
| 879 | self.assertGreaterEqual(1.1, 1.0) |
| 880 | self.assertGreaterEqual(1.0, 1.0) |
| 881 | self.assertLess(1.0, 1.1) |
| 882 | self.assertLessEqual(1.0, 1.1) |
| 883 | self.assertLessEqual(1.0, 1.0) |
| 884 | self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.1) |
| 885 | self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.0) |
| 886 | self.assertRaises(self.failureException, self.assertGreaterEqual, 1.0, 1.1) |
| 887 | self.assertRaises(self.failureException, self.assertLess, 1.1, 1.0) |
| 888 | self.assertRaises(self.failureException, self.assertLess, 1.0, 1.0) |
| 889 | self.assertRaises(self.failureException, self.assertLessEqual, 1.1, 1.0) |
| 890 | |
| 891 | # Try Strings |
| 892 | self.assertGreater('bug', 'ant') |
| 893 | self.assertGreaterEqual('bug', 'ant') |
| 894 | self.assertGreaterEqual('ant', 'ant') |
| 895 | self.assertLess('ant', 'bug') |
| 896 | self.assertLessEqual('ant', 'bug') |
| 897 | self.assertLessEqual('ant', 'ant') |
| 898 | self.assertRaises(self.failureException, self.assertGreater, 'ant', 'bug') |
| 899 | self.assertRaises(self.failureException, self.assertGreater, 'ant', 'ant') |
| 900 | self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 'bug') |
| 901 | self.assertRaises(self.failureException, self.assertLess, 'bug', 'ant') |
| 902 | self.assertRaises(self.failureException, self.assertLess, 'ant', 'ant') |
| 903 | self.assertRaises(self.failureException, self.assertLessEqual, 'bug', 'ant') |
| 904 | |
| 905 | # Try bytes |
| 906 | self.assertGreater(b'bug', b'ant') |
| 907 | self.assertGreaterEqual(b'bug', b'ant') |
| 908 | self.assertGreaterEqual(b'ant', b'ant') |
| 909 | self.assertLess(b'ant', b'bug') |
| 910 | self.assertLessEqual(b'ant', b'bug') |
| 911 | self.assertLessEqual(b'ant', b'ant') |
| 912 | self.assertRaises(self.failureException, self.assertGreater, b'ant', b'bug') |
| 913 | self.assertRaises(self.failureException, self.assertGreater, b'ant', b'ant') |
| 914 | self.assertRaises(self.failureException, self.assertGreaterEqual, b'ant', |
| 915 | b'bug') |
| 916 | self.assertRaises(self.failureException, self.assertLess, b'bug', b'ant') |
| 917 | self.assertRaises(self.failureException, self.assertLess, b'ant', b'ant') |
| 918 | self.assertRaises(self.failureException, self.assertLessEqual, b'bug', b'ant') |
| 919 | |
| 920 | def testAssertMultiLineEqual(self): |
| 921 | sample_text = """\ |
| 922 | http://www.python.org/doc/2.3/lib/module-unittest.html |
| 923 | test case |
| 924 | A test case is the smallest unit of testing. [...] |
| 925 | """ |
| 926 | revised_sample_text = """\ |
| 927 | http://www.python.org/doc/2.4.1/lib/module-unittest.html |
| 928 | test case |
| 929 | A test case is the smallest unit of testing. [...] You may provide your |
| 930 | own implementation that does not subclass from TestCase, of course. |
| 931 | """ |
Michael Foord | ac76074 | 2010-06-05 13:57:23 +0000 | [diff] [blame] | 932 | sample_text_error = """\ |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 933 | - http://www.python.org/doc/2.3/lib/module-unittest.html |
| 934 | ? ^ |
| 935 | + http://www.python.org/doc/2.4.1/lib/module-unittest.html |
| 936 | ? ^^^ |
| 937 | test case |
| 938 | - A test case is the smallest unit of testing. [...] |
| 939 | + A test case is the smallest unit of testing. [...] You may provide your |
| 940 | ? +++++++++++++++++++++ |
| 941 | + own implementation that does not subclass from TestCase, of course. |
| 942 | """ |
Michael Foord | ac76074 | 2010-06-05 13:57:23 +0000 | [diff] [blame] | 943 | self.maxDiff = None |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 944 | try: |
| 945 | self.assertMultiLineEqual(sample_text, revised_sample_text) |
| 946 | except self.failureException as e: |
Michael Foord | ac76074 | 2010-06-05 13:57:23 +0000 | [diff] [blame] | 947 | # need to remove the first line of the error message |
| 948 | error = str(e).split('\n', 1)[1] |
| 949 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 950 | # no fair testing ourself with ourself, and assertEqual is used for strings |
| 951 | # so can't use assertEqual either. Just use assertTrue. |
Michael Foord | ac76074 | 2010-06-05 13:57:23 +0000 | [diff] [blame] | 952 | self.assertTrue(sample_text_error == error) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 953 | |
Michael Foord | c653ce3 | 2010-07-10 13:52:22 +0000 | [diff] [blame] | 954 | def testAsertEqualSingleLine(self): |
| 955 | sample_text = "laden swallows fly slowly" |
| 956 | revised_sample_text = "unladen swallows fly quickly" |
| 957 | sample_text_error = """\ |
| 958 | - laden swallows fly slowly |
| 959 | ? ^^^^ |
| 960 | + unladen swallows fly quickly |
| 961 | ? ++ ^^^^^ |
| 962 | """ |
| 963 | try: |
| 964 | self.assertEqual(sample_text, revised_sample_text) |
| 965 | except self.failureException as e: |
| 966 | error = str(e).split('\n', 1)[1] |
| 967 | self.assertTrue(sample_text_error == error) |
| 968 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 969 | def testAssertIsNone(self): |
| 970 | self.assertIsNone(None) |
| 971 | self.assertRaises(self.failureException, self.assertIsNone, False) |
| 972 | self.assertIsNotNone('DjZoPloGears on Rails') |
| 973 | self.assertRaises(self.failureException, self.assertIsNotNone, None) |
| 974 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 975 | def testAssertRegex(self): |
| 976 | self.assertRegex('asdfabasdf', r'ab+') |
| 977 | self.assertRaises(self.failureException, self.assertRegex, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 978 | 'saaas', r'aaaa') |
| 979 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 980 | def testAssertRaisesRegex(self): |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 981 | class ExceptionMock(Exception): |
| 982 | pass |
| 983 | |
| 984 | def Stub(): |
| 985 | raise ExceptionMock('We expect') |
| 986 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 987 | self.assertRaisesRegex(ExceptionMock, re.compile('expect$'), Stub) |
| 988 | self.assertRaisesRegex(ExceptionMock, 'expect$', Stub) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 989 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 990 | def testAssertNotRaisesRegex(self): |
| 991 | self.assertRaisesRegex( |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 992 | self.failureException, '^Exception not raised by <lambda>$', |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 993 | self.assertRaisesRegex, Exception, re.compile('x'), |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 994 | lambda: None) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 995 | self.assertRaisesRegex( |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 996 | self.failureException, '^Exception not raised by <lambda>$', |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 997 | self.assertRaisesRegex, Exception, 'x', |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 998 | lambda: None) |
| 999 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1000 | def testAssertRaisesRegexMismatch(self): |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1001 | def Stub(): |
| 1002 | raise Exception('Unexpected') |
| 1003 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1004 | self.assertRaisesRegex( |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1005 | self.failureException, |
| 1006 | r'"\^Expected\$" does not match "Unexpected"', |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1007 | self.assertRaisesRegex, Exception, '^Expected$', |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1008 | Stub) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1009 | self.assertRaisesRegex( |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1010 | self.failureException, |
| 1011 | r'"\^Expected\$" does not match "Unexpected"', |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1012 | self.assertRaisesRegex, Exception, |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1013 | re.compile('^Expected$'), Stub) |
| 1014 | |
| 1015 | def testAssertRaisesExcValue(self): |
| 1016 | class ExceptionMock(Exception): |
| 1017 | pass |
| 1018 | |
| 1019 | def Stub(foo): |
| 1020 | raise ExceptionMock(foo) |
| 1021 | v = "particular value" |
| 1022 | |
| 1023 | ctx = self.assertRaises(ExceptionMock) |
| 1024 | with ctx: |
| 1025 | Stub(v) |
| 1026 | e = ctx.exception |
| 1027 | self.assertIsInstance(e, ExceptionMock) |
| 1028 | self.assertEqual(e.args[0], v) |
| 1029 | |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1030 | def testAssertWarnsCallable(self): |
| 1031 | def _runtime_warn(): |
| 1032 | warnings.warn("foo", RuntimeWarning) |
| 1033 | # Success when the right warning is triggered, even several times |
| 1034 | self.assertWarns(RuntimeWarning, _runtime_warn) |
| 1035 | self.assertWarns(RuntimeWarning, _runtime_warn) |
| 1036 | # A tuple of warning classes is accepted |
| 1037 | self.assertWarns((DeprecationWarning, RuntimeWarning), _runtime_warn) |
| 1038 | # *args and **kwargs also work |
| 1039 | self.assertWarns(RuntimeWarning, |
| 1040 | warnings.warn, "foo", category=RuntimeWarning) |
| 1041 | # Failure when no warning is triggered |
| 1042 | with self.assertRaises(self.failureException): |
| 1043 | self.assertWarns(RuntimeWarning, lambda: 0) |
| 1044 | # Failure when another warning is triggered |
| 1045 | with warnings.catch_warnings(): |
| 1046 | # Force default filter (in case tests are run with -We) |
| 1047 | warnings.simplefilter("default", RuntimeWarning) |
| 1048 | with self.assertRaises(self.failureException): |
| 1049 | self.assertWarns(DeprecationWarning, _runtime_warn) |
| 1050 | # Filters for other warnings are not modified |
| 1051 | with warnings.catch_warnings(): |
| 1052 | warnings.simplefilter("error", RuntimeWarning) |
| 1053 | with self.assertRaises(RuntimeWarning): |
| 1054 | self.assertWarns(DeprecationWarning, _runtime_warn) |
| 1055 | |
| 1056 | def testAssertWarnsContext(self): |
| 1057 | # Believe it or not, it is preferrable to duplicate all tests above, |
| 1058 | # to make sure the __warningregistry__ $@ is circumvented correctly. |
| 1059 | def _runtime_warn(): |
| 1060 | warnings.warn("foo", RuntimeWarning) |
| 1061 | _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1] |
| 1062 | with self.assertWarns(RuntimeWarning) as cm: |
| 1063 | _runtime_warn() |
| 1064 | # A tuple of warning classes is accepted |
| 1065 | with self.assertWarns((DeprecationWarning, RuntimeWarning)) as cm: |
| 1066 | _runtime_warn() |
| 1067 | # The context manager exposes various useful attributes |
| 1068 | self.assertIsInstance(cm.warning, RuntimeWarning) |
| 1069 | self.assertEqual(cm.warning.args[0], "foo") |
| 1070 | self.assertIn("test_case.py", cm.filename) |
| 1071 | self.assertEqual(cm.lineno, _runtime_warn_lineno + 1) |
| 1072 | # Same with several warnings |
| 1073 | with self.assertWarns(RuntimeWarning): |
| 1074 | _runtime_warn() |
| 1075 | _runtime_warn() |
| 1076 | with self.assertWarns(RuntimeWarning): |
| 1077 | warnings.warn("foo", category=RuntimeWarning) |
| 1078 | # Failure when no warning is triggered |
| 1079 | with self.assertRaises(self.failureException): |
| 1080 | with self.assertWarns(RuntimeWarning): |
| 1081 | pass |
| 1082 | # Failure when another warning is triggered |
| 1083 | with warnings.catch_warnings(): |
| 1084 | # Force default filter (in case tests are run with -We) |
| 1085 | warnings.simplefilter("default", RuntimeWarning) |
| 1086 | with self.assertRaises(self.failureException): |
| 1087 | with self.assertWarns(DeprecationWarning): |
| 1088 | _runtime_warn() |
| 1089 | # Filters for other warnings are not modified |
| 1090 | with warnings.catch_warnings(): |
| 1091 | warnings.simplefilter("error", RuntimeWarning) |
| 1092 | with self.assertRaises(RuntimeWarning): |
| 1093 | with self.assertWarns(DeprecationWarning): |
| 1094 | _runtime_warn() |
| 1095 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1096 | def testAssertWarnsRegexCallable(self): |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1097 | def _runtime_warn(msg): |
| 1098 | warnings.warn(msg, RuntimeWarning) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1099 | self.assertWarnsRegex(RuntimeWarning, "o+", |
| 1100 | _runtime_warn, "foox") |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1101 | # Failure when no warning is triggered |
| 1102 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1103 | self.assertWarnsRegex(RuntimeWarning, "o+", |
| 1104 | lambda: 0) |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1105 | # Failure when another warning is triggered |
| 1106 | with warnings.catch_warnings(): |
| 1107 | # Force default filter (in case tests are run with -We) |
| 1108 | warnings.simplefilter("default", RuntimeWarning) |
| 1109 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1110 | self.assertWarnsRegex(DeprecationWarning, "o+", |
| 1111 | _runtime_warn, "foox") |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1112 | # Failure when message doesn't match |
| 1113 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1114 | self.assertWarnsRegex(RuntimeWarning, "o+", |
| 1115 | _runtime_warn, "barz") |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1116 | # A little trickier: we ask RuntimeWarnings to be raised, and then |
| 1117 | # check for some of them. It is implementation-defined whether |
| 1118 | # non-matching RuntimeWarnings are simply re-raised, or produce a |
| 1119 | # failureException. |
| 1120 | with warnings.catch_warnings(): |
| 1121 | warnings.simplefilter("error", RuntimeWarning) |
| 1122 | with self.assertRaises((RuntimeWarning, self.failureException)): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1123 | self.assertWarnsRegex(RuntimeWarning, "o+", |
| 1124 | _runtime_warn, "barz") |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1125 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1126 | def testAssertWarnsRegexContext(self): |
| 1127 | # Same as above, but with assertWarnsRegex as a context manager |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1128 | def _runtime_warn(msg): |
| 1129 | warnings.warn(msg, RuntimeWarning) |
| 1130 | _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1] |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1131 | with self.assertWarnsRegex(RuntimeWarning, "o+") as cm: |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1132 | _runtime_warn("foox") |
| 1133 | self.assertIsInstance(cm.warning, RuntimeWarning) |
| 1134 | self.assertEqual(cm.warning.args[0], "foox") |
| 1135 | self.assertIn("test_case.py", cm.filename) |
| 1136 | self.assertEqual(cm.lineno, _runtime_warn_lineno + 1) |
| 1137 | # Failure when no warning is triggered |
| 1138 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1139 | with self.assertWarnsRegex(RuntimeWarning, "o+"): |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1140 | pass |
| 1141 | # Failure when another warning is triggered |
| 1142 | with warnings.catch_warnings(): |
| 1143 | # Force default filter (in case tests are run with -We) |
| 1144 | warnings.simplefilter("default", RuntimeWarning) |
| 1145 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1146 | with self.assertWarnsRegex(DeprecationWarning, "o+"): |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1147 | _runtime_warn("foox") |
| 1148 | # Failure when message doesn't match |
| 1149 | with self.assertRaises(self.failureException): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1150 | with self.assertWarnsRegex(RuntimeWarning, "o+"): |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1151 | _runtime_warn("barz") |
| 1152 | # A little trickier: we ask RuntimeWarnings to be raised, and then |
| 1153 | # check for some of them. It is implementation-defined whether |
| 1154 | # non-matching RuntimeWarnings are simply re-raised, or produce a |
| 1155 | # failureException. |
| 1156 | with warnings.catch_warnings(): |
| 1157 | warnings.simplefilter("error", RuntimeWarning) |
| 1158 | with self.assertRaises((RuntimeWarning, self.failureException)): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1159 | with self.assertWarnsRegex(RuntimeWarning, "o+"): |
Antoine Pitrou | 4bc12ef | 2010-09-06 19:25:46 +0000 | [diff] [blame] | 1160 | _runtime_warn("barz") |
| 1161 | |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1162 | def testDeprecatedMethodNames(self): |
Ezio Melotti | 361467e | 2011-04-03 17:37:58 +0300 | [diff] [blame] | 1163 | """ |
| 1164 | Test that the deprecated methods raise a DeprecationWarning. See #9424. |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1165 | """ |
| 1166 | old = ( |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1167 | (self.failIfEqual, (3, 5)), |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1168 | (self.assertNotEquals, (3, 5)), |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1169 | (self.failUnlessEqual, (3, 3)), |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1170 | (self.assertEquals, (3, 3)), |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1171 | (self.failUnlessAlmostEqual, (2.0, 2.0)), |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1172 | (self.assertAlmostEquals, (2.0, 2.0)), |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1173 | (self.failIfAlmostEqual, (3.0, 5.0)), |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1174 | (self.assertNotAlmostEquals, (3.0, 5.0)), |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1175 | (self.failUnless, (True,)), |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1176 | (self.assert_, (True,)), |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1177 | (self.failUnlessRaises, (TypeError, lambda _: 3.14 + 'spam')), |
| 1178 | (self.failIf, (False,)), |
| 1179 | (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] | 1180 | (self.assertRaisesRegexp, (KeyError, 'foo', lambda: {}['foo'])), |
| 1181 | (self.assertRegexpMatches, ('bar', 'bar')), |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1182 | ) |
| 1183 | for meth, args in old: |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1184 | with self.assertWarns(DeprecationWarning): |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1185 | meth(*args) |
Ezio Melotti | 2baf1a6 | 2010-11-22 12:56:58 +0000 | [diff] [blame] | 1186 | |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 1187 | # disable this test for now. When the version where the fail* methods will |
| 1188 | # be removed is decided, re-enable it and update the version |
| 1189 | def _testDeprecatedFailMethods(self): |
| 1190 | """Test that the deprecated fail* methods get removed in 3.x""" |
| 1191 | if sys.version_info[:2] < (3, 3): |
| 1192 | return |
| 1193 | deprecated_names = [ |
| 1194 | 'failIfEqual', 'failUnlessEqual', 'failUnlessAlmostEqual', |
| 1195 | 'failIfAlmostEqual', 'failUnless', 'failUnlessRaises', 'failIf', |
| 1196 | 'assertDictContainsSubset', |
| 1197 | ] |
| 1198 | for deprecated_name in deprecated_names: |
| 1199 | with self.assertRaises(AttributeError): |
| 1200 | getattr(self, deprecated_name) # remove these in 3.x |
| 1201 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 1202 | def testDeepcopy(self): |
| 1203 | # Issue: 5660 |
| 1204 | class TestableTest(unittest.TestCase): |
| 1205 | def testNothing(self): |
| 1206 | pass |
| 1207 | |
| 1208 | test = TestableTest('testNothing') |
| 1209 | |
| 1210 | # This shouldn't blow up |
| 1211 | deepcopy(test) |
Michael Foord | 8ca6d98 | 2010-11-20 15:34:26 +0000 | [diff] [blame] | 1212 | |
| 1213 | def testPickle(self): |
| 1214 | # Issue 10326 |
| 1215 | |
| 1216 | # Can't use TestCase classes defined in Test class as |
| 1217 | # pickle does not work with inner classes |
| 1218 | test = unittest.TestCase('run') |
| 1219 | for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1220 | |
| 1221 | # blew up prior to fix |
| 1222 | pickled_test = pickle.dumps(test, protocol=protocol) |
Michael Foord | 8a00eec | 2010-11-20 15:43:02 +0000 | [diff] [blame] | 1223 | unpickled_test = pickle.loads(pickled_test) |
| 1224 | self.assertEqual(test, unpickled_test) |
Michael Foord | b357fb7 | 2010-11-20 15:47:56 +0000 | [diff] [blame] | 1225 | |
| 1226 | # exercise the TestCase instance in a way that will invoke |
| 1227 | # the type equality lookup mechanism |
| 1228 | unpickled_test.assertEqual(set(), set()) |
Michael Foord | b3468f7 | 2010-12-19 03:19:47 +0000 | [diff] [blame] | 1229 | |
| 1230 | def testKeyboardInterrupt(self): |
| 1231 | def _raise(self=None): |
| 1232 | raise KeyboardInterrupt |
| 1233 | def nothing(self): |
| 1234 | pass |
| 1235 | |
| 1236 | class Test1(unittest.TestCase): |
| 1237 | test_something = _raise |
| 1238 | |
| 1239 | class Test2(unittest.TestCase): |
| 1240 | setUp = _raise |
| 1241 | test_something = nothing |
| 1242 | |
| 1243 | class Test3(unittest.TestCase): |
| 1244 | test_something = nothing |
| 1245 | tearDown = _raise |
| 1246 | |
| 1247 | class Test4(unittest.TestCase): |
| 1248 | def test_something(self): |
| 1249 | self.addCleanup(_raise) |
| 1250 | |
| 1251 | for klass in (Test1, Test2, Test3, Test4): |
| 1252 | with self.assertRaises(KeyboardInterrupt): |
| 1253 | klass('test_something').run() |
| 1254 | |
| 1255 | def testSkippingEverywhere(self): |
| 1256 | def _skip(self=None): |
| 1257 | raise unittest.SkipTest('some reason') |
| 1258 | def nothing(self): |
| 1259 | pass |
| 1260 | |
| 1261 | class Test1(unittest.TestCase): |
| 1262 | test_something = _skip |
| 1263 | |
| 1264 | class Test2(unittest.TestCase): |
| 1265 | setUp = _skip |
| 1266 | test_something = nothing |
| 1267 | |
| 1268 | class Test3(unittest.TestCase): |
| 1269 | test_something = nothing |
| 1270 | tearDown = _skip |
| 1271 | |
| 1272 | class Test4(unittest.TestCase): |
| 1273 | def test_something(self): |
| 1274 | self.addCleanup(_skip) |
| 1275 | |
| 1276 | for klass in (Test1, Test2, Test3, Test4): |
| 1277 | result = unittest.TestResult() |
| 1278 | klass('test_something').run(result) |
| 1279 | self.assertEqual(len(result.skipped), 1) |
| 1280 | self.assertEqual(result.testsRun, 1) |
| 1281 | |
| 1282 | def testSystemExit(self): |
| 1283 | def _raise(self=None): |
| 1284 | raise SystemExit |
| 1285 | def nothing(self): |
| 1286 | pass |
| 1287 | |
| 1288 | class Test1(unittest.TestCase): |
| 1289 | test_something = _raise |
| 1290 | |
| 1291 | class Test2(unittest.TestCase): |
| 1292 | setUp = _raise |
| 1293 | test_something = nothing |
| 1294 | |
| 1295 | class Test3(unittest.TestCase): |
| 1296 | test_something = nothing |
| 1297 | tearDown = _raise |
| 1298 | |
| 1299 | class Test4(unittest.TestCase): |
| 1300 | def test_something(self): |
| 1301 | self.addCleanup(_raise) |
| 1302 | |
| 1303 | for klass in (Test1, Test2, Test3, Test4): |
| 1304 | result = unittest.TestResult() |
| 1305 | klass('test_something').run(result) |
| 1306 | self.assertEqual(len(result.errors), 1) |
| 1307 | self.assertEqual(result.testsRun, 1) |
Benjamin Peterson | b6ffa79 | 2011-07-14 12:48:25 -0500 | [diff] [blame] | 1308 | |
| 1309 | @support.cpython_only |
| 1310 | def testNoCycles(self): |
| 1311 | case = unittest.TestCase() |
| 1312 | wr = weakref.ref(case) |
| 1313 | with support.disable_gc(): |
| 1314 | del case |
| 1315 | self.assertFalse(wr()) |