summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_unittest.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2008-12-28 14:29:00 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2008-12-28 14:29:00 (GMT)
commit5acd41e0a1663127ac0c01b00e934ee1e7f537a9 (patch)
treee0bd0969b40f38ce562bf1b2284110350ff176c7 /Lib/test/test_unittest.py
parentccc47b6eee267d6661c8521578e2fc9d53494029 (diff)
downloadcpython-5acd41e0a1663127ac0c01b00e934ee1e7f537a9.zip
cpython-5acd41e0a1663127ac0c01b00e934ee1e7f537a9.tar.gz
cpython-5acd41e0a1663127ac0c01b00e934ee1e7f537a9.tar.bz2
Merged revisions 67979 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r67979 | antoine.pitrou | 2008-12-28 15:09:36 +0100 (dim., 28 déc. 2008) | 3 lines Issue #4444: Allow assertRaises() to be used as a context handler. ........
Diffstat (limited to 'Lib/test/test_unittest.py')
-rw-r--r--Lib/test/test_unittest.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py
index c9c17d3..9c12205 100644
--- a/Lib/test/test_unittest.py
+++ b/Lib/test/test_unittest.py
@@ -2284,6 +2284,43 @@ class Test_Assertions(TestCase):
self.assertRaises(AssertionError,
self.failIfAlmostEqual, 0, .1+.1j, places=0)
+ def test_assertRaises(self):
+ def _raise(e):
+ raise e
+ self.assertRaises(KeyError, _raise, KeyError)
+ self.assertRaises(KeyError, _raise, KeyError("key"))
+ try:
+ self.assertRaises(KeyError, lambda: None)
+ except AssertionError as e:
+ self.assert_("KeyError not raised" in str(e), str(e))
+ else:
+ self.fail("assertRaises() didn't fail")
+ try:
+ self.assertRaises(KeyError, _raise, ValueError)
+ except ValueError:
+ pass
+ else:
+ self.fail("assertRaises() didn't let exception pass through")
+ with self.assertRaises(KeyError):
+ raise KeyError
+ with self.assertRaises(KeyError):
+ raise KeyError("key")
+ try:
+ with self.assertRaises(KeyError):
+ pass
+ except AssertionError as e:
+ self.assert_("KeyError not raised" in str(e), str(e))
+ else:
+ self.fail("assertRaises() didn't fail")
+ try:
+ with self.assertRaises(KeyError):
+ raise ValueError
+ except ValueError:
+ pass
+ else:
+ self.fail("assertRaises() didn't let exception pass through")
+
+
######################################################################
## Main
######################################################################