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