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 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): with py.test.raises(ZeroDivisionError):
x = 0 x = 0
1 / x 1 / x
with py.test.raises(RuntimeError) as excinfo:
call_something()
# do extra checks on excinfo.value|type|traceback objects
(thanks Ronny Pfannschmidt) (thanks Ronny Pfannschmidt)
- Funcarg factories can now dynamically apply a marker to a - 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): with py.test.raises(ZeroDivisionError):
1 / 0 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, 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:: 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 self.excinfo = None
def __enter__(self): def __enter__(self):
return self self.excinfo = object.__new__(py.code.ExceptionInfo)
return self.excinfo
def __exit__(self, *tp): def __exit__(self, *tp):
__tracebackhide__ = True __tracebackhide__ = True
@ -419,7 +420,7 @@ class RaisesContext(object):
raise ExceptionFailure(msg="DID NOT RAISE", raise ExceptionFailure(msg="DID NOT RAISE",
expr=(), expr=(),
expected=self.ExpectedException) expected=self.ExpectedException)
self.excinfo = py.code.ExceptionInfo(tp) self.excinfo.__init__(tp)
return issubclass(self.excinfo.type, self.ExpectedException) return issubclass(self.excinfo.type, self.ExpectedException)

View File

@ -347,12 +347,11 @@ class TestRaises:
import py import py
def test_simple(): 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 1/0
print (excinfo)
print ctx.excinfo assert excinfo.type == ZeroDivisionError
assert ctx.excinfo.type is ZeroDivisionError
def test_noraise(): def test_noraise():
with py.test.raises(py.test.raises.Exception): with py.test.raises(py.test.raises.Exception):