From 2bd52dcccbd38908ae2a3b3bdceb3b04d6b16c81 Mon Sep 17 00:00:00 2001 From: Michael Foord Date: Sun, 7 Feb 2010 18:44:12 +0000 Subject: [PATCH] assertRaises as context manager now allows you to access exception as documented --- Lib/test/test_unittest.py | 9 +++++++-- Lib/unittest/case.py | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py index ab043822da..04a7322b23 100644 --- a/Lib/test/test_unittest.py +++ b/Lib/test/test_unittest.py @@ -3059,8 +3059,13 @@ class Test_Assertions(TestCase): pass else: self.fail("assertRaises() didn't let exception pass through") - with self.assertRaises(KeyError): - raise KeyError + with self.assertRaises(KeyError) as cm: + try: + raise KeyError + except Exception, e: + raise + self.assertIs(cm.exception, e) + with self.assertRaises(KeyError): raise KeyError("key") try: diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py index 63408e35ac..4acfa6539a 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -91,7 +91,7 @@ class _AssertRaisesContext(object): self.expected_regexp = expected_regexp def __enter__(self): - pass + return self def __exit__(self, exc_type, exc_value, tb): if exc_type is None: -- 2.40.0