From 523704f890905fde1826c51ac3b6a590d8f015b1 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 9 Jun 2010 14:45:41 +0200 Subject: [PATCH] make py.test.raises as-VAR be an ExceptionInfo object but only initialize it after the block is finished. --HG-- branch : trunk --- CHANGELOG | 9 +++++++-- doc/test/features.txt | 9 +++++++++ py/_plugin/pytest_runner.py | 5 +++-- testing/plugin/test_pytest_runner.py | 9 ++++----- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 0ddacdf9b..93e6807d7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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): + 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 diff --git a/doc/test/features.txt b/doc/test/features.txt index b0d1e2350..ac296943a 100644 --- a/doc/test/features.txt +++ b/doc/test/features.txt @@ -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:: diff --git a/py/_plugin/pytest_runner.py b/py/_plugin/pytest_runner.py index 2bcb63b90..4e5edd988 100644 --- a/py/_plugin/pytest_runner.py +++ b/py/_plugin/pytest_runner.py @@ -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) diff --git a/testing/plugin/test_pytest_runner.py b/testing/plugin/test_pytest_runner.py index 551d8c437..03fefdf96 100644 --- a/testing/plugin/test_pytest_runner.py +++ b/testing/plugin/test_pytest_runner.py @@ -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):