mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Merged revisions 78091,78094,78109 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r78091 | georg.brandl | 2010-02-07 19:02:22 +0200 (Sun, 07 Feb 2010) | 1 line Rename "exc_value" attribute on assertRaises context manager to "exception". ........ r78094 | michael.foord | 2010-02-07 20:44:12 +0200 (Sun, 07 Feb 2010) | 1 line assertRaises as context manager now allows you to access exception as documented ........ r78109 | ezio.melotti | 2010-02-08 23:52:08 +0200 (Mon, 08 Feb 2010) | 1 line Fix exc_value -> exception in docstring ........
This commit is contained in:
parent
7b26d7f82f
commit
4900823027
3 changed files with 20 additions and 20 deletions
|
@ -895,24 +895,24 @@ Test cases
|
||||||
do_something()
|
do_something()
|
||||||
|
|
||||||
The context manager will store the caught exception object in its
|
The context manager will store the caught exception object in its
|
||||||
:attr:`exc_value` attribute. This can be useful if the intention
|
:attr:`exception` attribute. This can be useful if the intention
|
||||||
is to perform additional checks on the exception raised::
|
is to perform additional checks on the exception raised::
|
||||||
|
|
||||||
with self.assertRaises(SomeException) as cm:
|
with self.assertRaises(SomeException) as cm:
|
||||||
do_something()
|
do_something()
|
||||||
|
|
||||||
the_exception = cm.exc_value
|
the_exception = cm.exception
|
||||||
self.assertEqual(the_exception.error_code, 3)
|
self.assertEqual(the_exception.error_code, 3)
|
||||||
|
|
||||||
.. versionchanged:: 3.1
|
.. versionchanged:: 3.1
|
||||||
Added the ability to use :meth:`assertRaises` as a context manager.
|
Added the ability to use :meth:`assertRaises` as a context manager.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.2
|
||||||
|
Added the :attr:`exception` attribute.
|
||||||
|
|
||||||
.. deprecated:: 3.1
|
.. deprecated:: 3.1
|
||||||
:meth:`failUnlessRaises`.
|
:meth:`failUnlessRaises`.
|
||||||
|
|
||||||
.. versionchanged:: 3.2
|
|
||||||
Added the :attr:`exc_value` attribute.
|
|
||||||
|
|
||||||
|
|
||||||
.. method:: assertRaisesRegexp(exception, regexp[, callable, ...])
|
.. method:: assertRaisesRegexp(exception, regexp[, callable, ...])
|
||||||
|
|
||||||
|
|
|
@ -626,7 +626,6 @@ class Test_TestLoader(TestCase):
|
||||||
# a good chance that it won't be imported when this test is run
|
# a good chance that it won't be imported when this test is run
|
||||||
module_name = 'audioop'
|
module_name = 'audioop'
|
||||||
|
|
||||||
import sys
|
|
||||||
if module_name in sys.modules:
|
if module_name in sys.modules:
|
||||||
del sys.modules[module_name]
|
del sys.modules[module_name]
|
||||||
|
|
||||||
|
@ -1014,7 +1013,6 @@ class Test_TestLoader(TestCase):
|
||||||
# a good chance that it won't be imported when this test is run
|
# a good chance that it won't be imported when this test is run
|
||||||
module_name = 'audioop'
|
module_name = 'audioop'
|
||||||
|
|
||||||
import sys
|
|
||||||
if module_name in sys.modules:
|
if module_name in sys.modules:
|
||||||
del sys.modules[module_name]
|
del sys.modules[module_name]
|
||||||
|
|
||||||
|
@ -1972,8 +1970,6 @@ class Test_TestResult(TestCase):
|
||||||
# methods. Contains formatted tracebacks instead
|
# methods. Contains formatted tracebacks instead
|
||||||
# of sys.exc_info() results."
|
# of sys.exc_info() results."
|
||||||
def test_addFailure(self):
|
def test_addFailure(self):
|
||||||
import sys
|
|
||||||
|
|
||||||
class Foo(unittest.TestCase):
|
class Foo(unittest.TestCase):
|
||||||
def test_1(self):
|
def test_1(self):
|
||||||
pass
|
pass
|
||||||
|
@ -2022,8 +2018,6 @@ class Test_TestResult(TestCase):
|
||||||
# methods. Contains formatted tracebacks instead
|
# methods. Contains formatted tracebacks instead
|
||||||
# of sys.exc_info() results."
|
# of sys.exc_info() results."
|
||||||
def test_addError(self):
|
def test_addError(self):
|
||||||
import sys
|
|
||||||
|
|
||||||
class Foo(unittest.TestCase):
|
class Foo(unittest.TestCase):
|
||||||
def test_1(self):
|
def test_1(self):
|
||||||
pass
|
pass
|
||||||
|
@ -2861,7 +2855,7 @@ test case
|
||||||
ctx = self.assertRaises(ExceptionMock)
|
ctx = self.assertRaises(ExceptionMock)
|
||||||
with ctx:
|
with ctx:
|
||||||
Stub(v)
|
Stub(v)
|
||||||
e = ctx.exc_value
|
e = ctx.exception
|
||||||
self.assertIsInstance(e, ExceptionMock)
|
self.assertIsInstance(e, ExceptionMock)
|
||||||
self.assertEqual(e.args[0], v)
|
self.assertEqual(e.args[0], v)
|
||||||
|
|
||||||
|
@ -3047,8 +3041,14 @@ class Test_Assertions(TestCase):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
self.fail("assertRaises() didn't let exception pass through")
|
self.fail("assertRaises() didn't let exception pass through")
|
||||||
with self.assertRaises(KeyError):
|
with self.assertRaises(KeyError) as cm:
|
||||||
raise KeyError
|
try:
|
||||||
|
raise KeyError
|
||||||
|
except Exception as e:
|
||||||
|
exc = e
|
||||||
|
raise
|
||||||
|
self.assertIs(cm.exception, exc)
|
||||||
|
|
||||||
with self.assertRaises(KeyError):
|
with self.assertRaises(KeyError):
|
||||||
raise KeyError("key")
|
raise KeyError("key")
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -99,7 +99,7 @@ class _AssertRaisesContext(object):
|
||||||
self.expected_regex = expected_regexp
|
self.expected_regex = expected_regexp
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
pass
|
return self
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, tb):
|
def __exit__(self, exc_type, exc_value, tb):
|
||||||
if exc_type is None:
|
if exc_type is None:
|
||||||
|
@ -116,8 +116,8 @@ class _AssertRaisesContext(object):
|
||||||
if not issubclass(exc_type, self.expected):
|
if not issubclass(exc_type, self.expected):
|
||||||
# let unexpected exceptions pass through
|
# let unexpected exceptions pass through
|
||||||
return False
|
return False
|
||||||
#store exception, without traceback, for later retrieval
|
# store exception, without traceback, for later retrieval
|
||||||
self.exc_value = exc_value.with_traceback(None)
|
self.exception = exc_value.with_traceback(None)
|
||||||
if self.expected_regex is None:
|
if self.expected_regex is None:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -397,12 +397,12 @@ class TestCase(object):
|
||||||
do_something()
|
do_something()
|
||||||
|
|
||||||
The context manager keeps a reference to the exception as
|
The context manager keeps a reference to the exception as
|
||||||
the exc_value attribute. This allows you to inspect the
|
the 'exception' attribute. This allows you to inspect the
|
||||||
exception after the assertion::
|
exception after the assertion::
|
||||||
|
|
||||||
with self.assertRaises(SomeException) as cm:
|
with self.assertRaises(SomeException) as cm:
|
||||||
do_something()
|
do_something()
|
||||||
the_exception = cm.exc_value
|
the_exception = cm.exception
|
||||||
self.assertEqual(the_exception.error_code, 3)
|
self.assertEqual(the_exception.error_code, 3)
|
||||||
"""
|
"""
|
||||||
context = _AssertRaisesContext(excClass, self, callableObj)
|
context = _AssertRaisesContext(excClass, self, callableObj)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue