[pypy-commit] pypy py3.6: add rudimentary context manager for "with pytest.raises()" in apptests

mattip pypy.commits at gmail.com
Sun Apr 14 18:38:01 EDT 2019


Author: Matti Picus <matti.picus at gmail.com>
Branch: py3.6
Changeset: r96494:0f6d909ffb0c
Date: 2019-04-15 01:37 +0300
http://bitbucket.org/pypy/pypy/changeset/0f6d909ffb0c/

Log:	add rudimentary context manager for "with pytest.raises()" in
	apptests

diff --git a/pypy/tool/pytest/apptest.py b/pypy/tool/pytest/apptest.py
--- a/pypy/tool/pytest/apptest.py
+++ b/pypy/tool/pytest/apptest.py
@@ -83,7 +83,11 @@
     __builtins__.py3k_skip = skip
     class ExceptionWrapper:
         pass
-    def raises(exc, func, *args, **kwargs):
+    def raises(exc, *args, **kwargs):
+        if not args:
+            return RaisesContext(exc)
+        func = args[0]
+        args = args[1:]
         import os
         try:
             if isinstance(func, str):
@@ -101,6 +105,22 @@
             return res
         else:
             raise AssertionError("DID NOT RAISE")
+
+    class RaisesContext(object):
+        def __init__(self, expected_exception):
+            self.expected_exception = expected_exception
+            self.excinfo = None
+
+        def __enter__(self):
+            return self
+
+        def __exit__(self, *tp):
+            __tracebackhide__ = True
+            if tp[0] is None:
+                pytest.fail("DID NOT RAISE")
+            self.value = tp[1]
+            return issubclass(tp[0], self.expected_exception)
+    
     __builtins__.raises = raises
     class Test:
         pass


More information about the pypy-commit mailing list