test_assertions.py 17 KB

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