make py.test.raises as-VAR be an ExceptionInfo object

but only initialize it after the block is finished.

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-06-09 14:45:41 +02:00
parent 6951da7da0
commit 523704f890
4 changed files with 23 additions and 9 deletions

View File

@ -4,12 +4,17 @@ Changes between 1.3.1 and 1.3.x
New features
++++++++++++++++++
- fix issue103: introduce additional "with py.test.raises(exc):" form, example::
- fix issue103: introduce py.test.raises as context manager, examples::
with py.test.raises(ZeroDivisionError):
x = 0
1 / x
with py.test.raises(RuntimeError) as excinfo:
call_something()
# do extra checks on excinfo.value|type|traceback objects
(thanks Ronny Pfannschmidt)
- Funcarg factories can now dynamically apply a marker to a

View File

@ -136,6 +136,15 @@ In order to write assertions about exceptions, you can use
with py.test.raises(ZeroDivisionError):
1 / 0
and if you need to have access to the actual exception info you may use::
with py.test.raises(RuntimeError) as excinfo:
def f():
f()
f()
# do checks related to excinfo.type, excinfo.value, excinfo.traceback
If you want to write test code that works on Python2.4 as well,
you may also use two other ways to test for an expected exception::

View File

@ -411,7 +411,8 @@ class RaisesContext(object):
self.excinfo = None
def __enter__(self):
return self
self.excinfo = object.__new__(py.code.ExceptionInfo)
return self.excinfo
def __exit__(self, *tp):
__tracebackhide__ = True
@ -419,7 +420,7 @@ class RaisesContext(object):
raise ExceptionFailure(msg="DID NOT RAISE",
expr=(),
expected=self.ExpectedException)
self.excinfo = py.code.ExceptionInfo(tp)
self.excinfo.__init__(tp)
return issubclass(self.excinfo.type, self.ExpectedException)

View File

@ -347,12 +347,11 @@ class TestRaises:
import py
def test_simple():
with py.test.raises(ZeroDivisionError) as ctx:
with py.test.raises(ZeroDivisionError) as excinfo:
assert isinstance(excinfo, py.code.ExceptionInfo)
1/0
print ctx.excinfo
assert ctx.excinfo.type is ZeroDivisionError
print (excinfo)
assert excinfo.type == ZeroDivisionError
def test_noraise():
with py.test.raises(py.test.raises.Exception):