[py-svn] py-trunk commit ad700c1825d3: make py.test.raises as-VAR be an ExceptionInfo object

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Jun 9 14:40:46 CEST 2010


# HG changeset patch -- Bitbucket.org
# Project py-trunk
# URL http://bitbucket.org/hpk42/py-trunk/overview
# User holger krekel <holger at merlinux.eu>
# Date 1276087541 -7200
# Node ID ad700c1825d31578cfb2e8db153d3d4e4e7b15ea
# Parent  c53778a0ed728a575f2c38066aa882b7e40b8e3a
make py.test.raises as-VAR be an ExceptionInfo object
but only initialize it after the block is finished.

--- 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):

--- 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)
 
 

--- a/doc/test/features.txt
+++ b/doc/test/features.txt
@@ -136,6 +136,15 @@ In order to write assertions about excep
     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::
 

--- 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



More information about the pytest-commit mailing list