Index: Doc/library/unittest.rst =================================================================== --- Doc/library/unittest.rst (revision 73271) +++ Doc/library/unittest.rst (working copy) @@ -888,6 +888,9 @@ with self.failUnlessRaises(some_error_class): do_something() + The context manager will store the caught exception object in its + :attr:`exc_value` attribute. + .. versionchanged:: 2.7 Added the ability to use :meth:`assertRaises` as a context manager. Index: Lib/test/test_unittest.py =================================================================== --- Lib/test/test_unittest.py (revision 73271) +++ Lib/test/test_unittest.py (working copy) @@ -2820,6 +2820,21 @@ self.assertRaisesRegexp, Exception, re.compile('^Expected$'), Stub) + def testAssertRaisesExcValue(self): + class ExceptionMock(Exception): + pass + + def Stub(foo): + raise ExceptionMock(foo) + v = "particular value" + + ctx = self.assertRaises(ExceptionMock) + with ctx: + Stub(v) + e = ctx.exc_value + self.assertTrue(isinstance(e, ExceptionMock)) + self.assertEqual(e.args[0], v) + def testSynonymAssertMethodNames(self): """Test undocumented method name synonyms. Index: Lib/unittest.py =================================================================== --- Lib/unittest.py (revision 73271) +++ Lib/unittest.py (working copy) @@ -291,6 +291,9 @@ if not issubclass(exc_type, self.expected): # let unexpected exceptions pass through return False + + self.exc_value = exc_value #store for later retrieval + if self.expected_regex is None: return True