Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 1 | import datetime |
Victor Stinner | cae969e | 2011-01-03 23:56:12 +0000 | [diff] [blame] | 2 | import warnings |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 3 | import unittest |
Ezio Melotti | b4dc250 | 2011-05-06 15:01:41 +0300 | [diff] [blame^] | 4 | from itertools import product |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 5 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 6 | |
| 7 | class Test_Assertions(unittest.TestCase): |
| 8 | def test_AlmostEqual(self): |
| 9 | self.assertAlmostEqual(1.00000001, 1.0) |
| 10 | self.assertNotAlmostEqual(1.0000001, 1.0) |
| 11 | self.assertRaises(self.failureException, |
| 12 | self.assertAlmostEqual, 1.0000001, 1.0) |
| 13 | self.assertRaises(self.failureException, |
| 14 | self.assertNotAlmostEqual, 1.00000001, 1.0) |
| 15 | |
| 16 | self.assertAlmostEqual(1.1, 1.0, places=0) |
| 17 | self.assertRaises(self.failureException, |
| 18 | self.assertAlmostEqual, 1.1, 1.0, places=1) |
| 19 | |
| 20 | self.assertAlmostEqual(0, .1+.1j, places=0) |
| 21 | self.assertNotAlmostEqual(0, .1+.1j, places=1) |
| 22 | self.assertRaises(self.failureException, |
| 23 | self.assertAlmostEqual, 0, .1+.1j, places=1) |
| 24 | self.assertRaises(self.failureException, |
| 25 | self.assertNotAlmostEqual, 0, .1+.1j, places=0) |
| 26 | |
| 27 | self.assertAlmostEqual(float('inf'), float('inf')) |
| 28 | self.assertRaises(self.failureException, self.assertNotAlmostEqual, |
| 29 | float('inf'), float('inf')) |
| 30 | |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 31 | def test_AmostEqualWithDelta(self): |
| 32 | self.assertAlmostEqual(1.1, 1.0, delta=0.5) |
| 33 | self.assertAlmostEqual(1.0, 1.1, delta=0.5) |
| 34 | self.assertNotAlmostEqual(1.1, 1.0, delta=0.05) |
| 35 | self.assertNotAlmostEqual(1.0, 1.1, delta=0.05) |
| 36 | |
| 37 | self.assertRaises(self.failureException, self.assertAlmostEqual, |
| 38 | 1.1, 1.0, delta=0.05) |
| 39 | self.assertRaises(self.failureException, self.assertNotAlmostEqual, |
| 40 | 1.1, 1.0, delta=0.5) |
| 41 | |
| 42 | self.assertRaises(TypeError, self.assertAlmostEqual, |
| 43 | 1.1, 1.0, places=2, delta=2) |
| 44 | self.assertRaises(TypeError, self.assertNotAlmostEqual, |
| 45 | 1.1, 1.0, places=2, delta=2) |
| 46 | |
| 47 | first = datetime.datetime.now() |
| 48 | second = first + datetime.timedelta(seconds=10) |
| 49 | self.assertAlmostEqual(first, second, |
| 50 | delta=datetime.timedelta(seconds=20)) |
| 51 | self.assertNotAlmostEqual(first, second, |
| 52 | delta=datetime.timedelta(seconds=5)) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 53 | |
| 54 | def test_assertRaises(self): |
| 55 | def _raise(e): |
| 56 | raise e |
| 57 | self.assertRaises(KeyError, _raise, KeyError) |
| 58 | self.assertRaises(KeyError, _raise, KeyError("key")) |
| 59 | try: |
| 60 | self.assertRaises(KeyError, lambda: None) |
| 61 | except self.failureException as e: |
| 62 | self.assertIn("KeyError not raised", str(e)) |
| 63 | else: |
| 64 | self.fail("assertRaises() didn't fail") |
| 65 | try: |
| 66 | self.assertRaises(KeyError, _raise, ValueError) |
| 67 | except ValueError: |
| 68 | pass |
| 69 | else: |
| 70 | self.fail("assertRaises() didn't let exception pass through") |
| 71 | with self.assertRaises(KeyError) as cm: |
| 72 | try: |
| 73 | raise KeyError |
| 74 | except Exception as e: |
| 75 | exc = e |
| 76 | raise |
| 77 | self.assertIs(cm.exception, exc) |
| 78 | |
| 79 | with self.assertRaises(KeyError): |
| 80 | raise KeyError("key") |
| 81 | try: |
| 82 | with self.assertRaises(KeyError): |
| 83 | pass |
| 84 | except self.failureException as e: |
| 85 | self.assertIn("KeyError not raised", str(e)) |
| 86 | else: |
| 87 | self.fail("assertRaises() didn't fail") |
| 88 | try: |
| 89 | with self.assertRaises(KeyError): |
| 90 | raise ValueError |
| 91 | except ValueError: |
| 92 | pass |
| 93 | else: |
| 94 | self.fail("assertRaises() didn't let exception pass through") |
| 95 | |
Ezio Melotti | 8f77630 | 2010-12-10 02:32:05 +0000 | [diff] [blame] | 96 | def testAssertNotRegex(self): |
| 97 | self.assertNotRegex('Ala ma kota', r'r+') |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 98 | try: |
Ezio Melotti | 8f77630 | 2010-12-10 02:32:05 +0000 | [diff] [blame] | 99 | self.assertNotRegex('Ala ma kota', r'k.t', 'Message') |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 100 | except self.failureException as e: |
| 101 | self.assertIn("'kot'", e.args[0]) |
| 102 | self.assertIn('Message', e.args[0]) |
| 103 | else: |
Ezio Melotti | 8f77630 | 2010-12-10 02:32:05 +0000 | [diff] [blame] | 104 | self.fail('assertNotRegex should have failed.') |
Benjamin Peterson | b48af54 | 2010-04-11 20:43:16 +0000 | [diff] [blame] | 105 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 106 | |
| 107 | class TestLongMessage(unittest.TestCase): |
| 108 | """Test that the individual asserts honour longMessage. |
| 109 | This actually tests all the message behaviour for |
| 110 | asserts that use longMessage.""" |
| 111 | |
| 112 | def setUp(self): |
| 113 | class TestableTestFalse(unittest.TestCase): |
| 114 | longMessage = False |
| 115 | failureException = self.failureException |
| 116 | |
| 117 | def testTest(self): |
| 118 | pass |
| 119 | |
| 120 | class TestableTestTrue(unittest.TestCase): |
| 121 | longMessage = True |
| 122 | failureException = self.failureException |
| 123 | |
| 124 | def testTest(self): |
| 125 | pass |
| 126 | |
| 127 | self.testableTrue = TestableTestTrue('testTest') |
| 128 | self.testableFalse = TestableTestFalse('testTest') |
| 129 | |
| 130 | def testDefault(self): |
Michael Foord | 5074df6 | 2010-12-03 00:53:09 +0000 | [diff] [blame] | 131 | self.assertTrue(unittest.TestCase.longMessage) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 132 | |
| 133 | def test_formatMsg(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 134 | self.assertEqual(self.testableFalse._formatMessage(None, "foo"), "foo") |
| 135 | self.assertEqual(self.testableFalse._formatMessage("foo", "bar"), "foo") |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 136 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 137 | self.assertEqual(self.testableTrue._formatMessage(None, "foo"), "foo") |
| 138 | self.assertEqual(self.testableTrue._formatMessage("foo", "bar"), "bar : foo") |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 139 | |
| 140 | # This blows up if _formatMessage uses string concatenation |
| 141 | self.testableTrue._formatMessage(object(), 'foo') |
| 142 | |
| 143 | def test_formatMessage_unicode_error(self): |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 144 | one = ''.join(chr(i) for i in range(255)) |
| 145 | # this used to cause a UnicodeDecodeError constructing msg |
| 146 | self.testableTrue._formatMessage(one, '\uFFFD') |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 147 | |
| 148 | def assertMessages(self, methodName, args, errors): |
Ezio Melotti | b4dc250 | 2011-05-06 15:01:41 +0300 | [diff] [blame^] | 149 | """ |
| 150 | Check that methodName(*args) raises the correct error messages. |
| 151 | errors should be a list of 4 regex that match the error when: |
| 152 | 1) longMessage = False and no msg passed; |
| 153 | 2) longMessage = False and msg passed; |
| 154 | 3) longMessage = True and no msg passed; |
| 155 | 4) longMessage = True and msg passed; |
| 156 | """ |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 157 | def getMethod(i): |
| 158 | useTestableFalse = i < 2 |
| 159 | if useTestableFalse: |
| 160 | test = self.testableFalse |
| 161 | else: |
| 162 | test = self.testableTrue |
| 163 | return getattr(test, methodName) |
| 164 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 165 | for i, expected_regex in enumerate(errors): |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 166 | testMethod = getMethod(i) |
| 167 | kwargs = {} |
| 168 | withMsg = i % 2 |
| 169 | if withMsg: |
| 170 | kwargs = {"msg": "oops"} |
| 171 | |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 172 | with self.assertRaisesRegex(self.failureException, |
| 173 | expected_regex=expected_regex): |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 174 | testMethod(*args, **kwargs) |
| 175 | |
| 176 | def testAssertTrue(self): |
| 177 | self.assertMessages('assertTrue', (False,), |
Ezio Melotti | 3044fa7 | 2010-12-18 17:31:58 +0000 | [diff] [blame] | 178 | ["^False is not true$", "^oops$", "^False is not true$", |
| 179 | "^False is not true : oops$"]) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 180 | |
| 181 | def testAssertFalse(self): |
| 182 | self.assertMessages('assertFalse', (True,), |
Ezio Melotti | 3044fa7 | 2010-12-18 17:31:58 +0000 | [diff] [blame] | 183 | ["^True is not false$", "^oops$", "^True is not false$", |
| 184 | "^True is not false : oops$"]) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 185 | |
| 186 | def testNotEqual(self): |
| 187 | self.assertMessages('assertNotEqual', (1, 1), |
| 188 | ["^1 == 1$", "^oops$", "^1 == 1$", |
| 189 | "^1 == 1 : oops$"]) |
| 190 | |
| 191 | def testAlmostEqual(self): |
| 192 | self.assertMessages('assertAlmostEqual', (1, 2), |
| 193 | ["^1 != 2 within 7 places$", "^oops$", |
| 194 | "^1 != 2 within 7 places$", "^1 != 2 within 7 places : oops$"]) |
| 195 | |
| 196 | def testNotAlmostEqual(self): |
| 197 | self.assertMessages('assertNotAlmostEqual', (1, 1), |
| 198 | ["^1 == 1 within 7 places$", "^oops$", |
| 199 | "^1 == 1 within 7 places$", "^1 == 1 within 7 places : oops$"]) |
| 200 | |
| 201 | def test_baseAssertEqual(self): |
| 202 | self.assertMessages('_baseAssertEqual', (1, 2), |
| 203 | ["^1 != 2$", "^oops$", "^1 != 2$", "^1 != 2 : oops$"]) |
| 204 | |
| 205 | def testAssertSequenceEqual(self): |
| 206 | # Error messages are multiline so not testing on full message |
| 207 | # assertTupleEqual and assertListEqual delegate to this method |
| 208 | self.assertMessages('assertSequenceEqual', ([], [None]), |
| 209 | ["\+ \[None\]$", "^oops$", r"\+ \[None\]$", |
| 210 | r"\+ \[None\] : oops$"]) |
| 211 | |
| 212 | def testAssertSetEqual(self): |
| 213 | self.assertMessages('assertSetEqual', (set(), set([None])), |
| 214 | ["None$", "^oops$", "None$", |
| 215 | "None : oops$"]) |
| 216 | |
| 217 | def testAssertIn(self): |
| 218 | self.assertMessages('assertIn', (None, []), |
| 219 | ['^None not found in \[\]$', "^oops$", |
| 220 | '^None not found in \[\]$', |
| 221 | '^None not found in \[\] : oops$']) |
| 222 | |
| 223 | def testAssertNotIn(self): |
| 224 | self.assertMessages('assertNotIn', (None, [None]), |
| 225 | ['^None unexpectedly found in \[None\]$', "^oops$", |
| 226 | '^None unexpectedly found in \[None\]$', |
| 227 | '^None unexpectedly found in \[None\] : oops$']) |
| 228 | |
| 229 | def testAssertDictEqual(self): |
| 230 | self.assertMessages('assertDictEqual', ({}, {'key': 'value'}), |
| 231 | [r"\+ \{'key': 'value'\}$", "^oops$", |
| 232 | "\+ \{'key': 'value'\}$", |
| 233 | "\+ \{'key': 'value'\} : oops$"]) |
| 234 | |
Ezio Melotti | 0f53501 | 2011-04-03 18:02:13 +0300 | [diff] [blame] | 235 | def testAssertDictContainsSubset(self): |
| 236 | with warnings.catch_warnings(): |
| 237 | warnings.simplefilter("ignore", DeprecationWarning) |
| 238 | |
| 239 | self.assertMessages('assertDictContainsSubset', ({'key': 'value'}, {}), |
| 240 | ["^Missing: 'key'$", "^oops$", |
| 241 | "^Missing: 'key'$", |
| 242 | "^Missing: 'key' : oops$"]) |
| 243 | |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 244 | def testAssertMultiLineEqual(self): |
| 245 | self.assertMessages('assertMultiLineEqual', ("", "foo"), |
| 246 | [r"\+ foo$", "^oops$", |
| 247 | r"\+ foo$", |
| 248 | r"\+ foo : oops$"]) |
| 249 | |
| 250 | def testAssertLess(self): |
| 251 | self.assertMessages('assertLess', (2, 1), |
| 252 | ["^2 not less than 1$", "^oops$", |
| 253 | "^2 not less than 1$", "^2 not less than 1 : oops$"]) |
| 254 | |
| 255 | def testAssertLessEqual(self): |
| 256 | self.assertMessages('assertLessEqual', (2, 1), |
| 257 | ["^2 not less than or equal to 1$", "^oops$", |
| 258 | "^2 not less than or equal to 1$", |
| 259 | "^2 not less than or equal to 1 : oops$"]) |
| 260 | |
| 261 | def testAssertGreater(self): |
| 262 | self.assertMessages('assertGreater', (1, 2), |
| 263 | ["^1 not greater than 2$", "^oops$", |
| 264 | "^1 not greater than 2$", |
| 265 | "^1 not greater than 2 : oops$"]) |
| 266 | |
| 267 | def testAssertGreaterEqual(self): |
| 268 | self.assertMessages('assertGreaterEqual', (1, 2), |
| 269 | ["^1 not greater than or equal to 2$", "^oops$", |
| 270 | "^1 not greater than or equal to 2$", |
| 271 | "^1 not greater than or equal to 2 : oops$"]) |
| 272 | |
| 273 | def testAssertIsNone(self): |
| 274 | self.assertMessages('assertIsNone', ('not None',), |
| 275 | ["^'not None' is not None$", "^oops$", |
| 276 | "^'not None' is not None$", |
| 277 | "^'not None' is not None : oops$"]) |
| 278 | |
| 279 | def testAssertIsNotNone(self): |
| 280 | self.assertMessages('assertIsNotNone', (None,), |
| 281 | ["^unexpectedly None$", "^oops$", |
| 282 | "^unexpectedly None$", |
| 283 | "^unexpectedly None : oops$"]) |
| 284 | |
| 285 | def testAssertIs(self): |
| 286 | self.assertMessages('assertIs', (None, 'foo'), |
| 287 | ["^None is not 'foo'$", "^oops$", |
| 288 | "^None is not 'foo'$", |
| 289 | "^None is not 'foo' : oops$"]) |
| 290 | |
| 291 | def testAssertIsNot(self): |
| 292 | self.assertMessages('assertIsNot', (None, None), |
| 293 | ["^unexpectedly identical: None$", "^oops$", |
| 294 | "^unexpectedly identical: None$", |
| 295 | "^unexpectedly identical: None : oops$"]) |
Ezio Melotti | b4dc250 | 2011-05-06 15:01:41 +0300 | [diff] [blame^] | 296 | |
| 297 | |
| 298 | def assertMessagesCM(self, methodName, args, func, errors): |
| 299 | """ |
| 300 | Check that the correct error messages are raised while executing: |
| 301 | with method(*args): |
| 302 | func() |
| 303 | *errors* should be a list of 4 regex that match the error when: |
| 304 | 1) longMessage = False and no msg passed; |
| 305 | 2) longMessage = False and msg passed; |
| 306 | 3) longMessage = True and no msg passed; |
| 307 | 4) longMessage = True and msg passed; |
| 308 | """ |
| 309 | p = product((self.testableFalse, self.testableTrue), |
| 310 | ({}, {"msg": "oops"})) |
| 311 | for (cls, kwargs), err in zip(p, errors): |
| 312 | method = getattr(cls, methodName) |
| 313 | with self.assertRaisesRegex(cls.failureException, err): |
| 314 | with method(*args, **kwargs) as cm: |
| 315 | func() |
| 316 | |
| 317 | def testAssertRaises(self): |
| 318 | self.assertMessagesCM('assertRaises', (TypeError,), lambda: None, |
| 319 | ['^TypeError not raised$', '^oops$', |
| 320 | '^TypeError not raised$', |
| 321 | '^TypeError not raised : oops$']) |
| 322 | |
| 323 | def testAssertRaisesRegex(self): |
| 324 | # test error not raised |
| 325 | self.assertMessagesCM('assertRaisesRegex', (TypeError, 'unused regex'), |
| 326 | lambda: None, |
| 327 | ['^TypeError not raised$', '^oops$', |
| 328 | '^TypeError not raised$', |
| 329 | '^TypeError not raised : oops$']) |
| 330 | # test error raised but with wrong message |
| 331 | def raise_wrong_message(): |
| 332 | raise TypeError('foo') |
| 333 | self.assertMessagesCM('assertRaisesRegex', (TypeError, 'regex'), |
| 334 | raise_wrong_message, |
| 335 | ['^"regex" does not match "foo"$', '^oops$', |
| 336 | '^"regex" does not match "foo"$', |
| 337 | '^"regex" does not match "foo" : oops$']) |
| 338 | |
| 339 | def testAssertWarns(self): |
| 340 | self.assertMessagesCM('assertWarns', (UserWarning,), lambda: None, |
| 341 | ['^UserWarning not triggered$', '^oops$', |
| 342 | '^UserWarning not triggered$', |
| 343 | '^UserWarning not triggered : oops$']) |
| 344 | |
| 345 | def testAssertWarnsRegex(self): |
| 346 | # test error not raised |
| 347 | self.assertMessagesCM('assertWarnsRegex', (UserWarning, 'unused regex'), |
| 348 | lambda: None, |
| 349 | ['^UserWarning not triggered$', '^oops$', |
| 350 | '^UserWarning not triggered$', |
| 351 | '^UserWarning not triggered : oops$']) |
| 352 | # test warning raised but with wrong message |
| 353 | def raise_wrong_message(): |
| 354 | warnings.warn('foo') |
| 355 | self.assertMessagesCM('assertWarnsRegex', (UserWarning, 'regex'), |
| 356 | raise_wrong_message, |
| 357 | ['^"regex" does not match "foo"$', '^oops$', |
| 358 | '^"regex" does not match "foo"$', |
| 359 | '^"regex" does not match "foo" : oops$']) |