[pypy-commit] pypy hypothesis-apptest: try to make it possible to test applevel functions (and soon methods) with

cfbolz pypy.commits at gmail.com
Fri Jun 10 08:46:24 EDT 2016


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: hypothesis-apptest
Changeset: r85074:1fb5d21e02ba
Date: 2016-06-09 14:42 +0200
http://bitbucket.org/pypy/pypy/changeset/1fb5d21e02ba/

Log:	try to make it possible to test applevel functions (and soon
	methods) with hypothesis. Limited to one argument for now.

diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -1121,7 +1121,7 @@
     if not isinstance(source, str):
         flags = source.__code__.co_flags
         source = py.std.inspect.getsource(source).lstrip()
-        while source.startswith(('@py.test.mark.', '@pytest.mark.')):
+        while source.startswith(('@py.test.mark.', '@pytest.mark.', '@app_hypothesis_given')):
             # these decorators are known to return the same function
             # object, we may ignore them
             assert '\n' in source
diff --git a/pypy/objspace/std/test/test_mapdict.py b/pypy/objspace/std/test/test_mapdict.py
--- a/pypy/objspace/std/test/test_mapdict.py
+++ b/pypy/objspace/std/test/test_mapdict.py
@@ -1,5 +1,6 @@
 from pypy.objspace.std.test.test_dictmultiobject import FakeSpace, W_DictObject
 from pypy.objspace.std.mapdict import *
+from pypy.tool.pytest.appsupport import app_hypothesis_given
 
 class Config:
     class objspace:
diff --git a/pypy/tool/pytest/appsupport.py b/pypy/tool/pytest/appsupport.py
--- a/pypy/tool/pytest/appsupport.py
+++ b/pypy/tool/pytest/appsupport.py
@@ -11,6 +11,21 @@
 
 # ____________________________________________________________
 
+def app_hypothesis_given(arg1):
+    from hypothesis import given
+    def decorator(func):
+        @given(arg1)
+        def inner(space, original, arg1):
+            return original(space, space.wrap(arg1))
+
+        @given(arg1)
+        def appdirect(arg1):
+            return func(arg1)
+        appdirect.hypothesis_inner = inner
+        appdirect.original_function = func
+        return appdirect
+    return decorator
+
 class AppCode(object):
     def __init__(self, space, pycode):
         self.code = pycode
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
@@ -51,10 +51,17 @@
         if self.config.option.runappdirect:
             return target()
         space = gettestobjspace()
-        filename = self._getdynfilename(target)
-        func = app2interp_temp(target, filename=filename)
+        if hasattr(target, 'hypothesis_inner'):
+            filename = self._getdynfilename(target.original_function)
+            original = app2interp_temp(target.original_function, filename=filename)
+            func = target.hypothesis_inner
+            args = (space, original)
+        else:
+            filename = self._getdynfilename(target)
+            args = (space, )
+            func = app2interp_temp(target, filename=filename)
         print "executing", func
-        self.execute_appex(space, func, space)
+        self.execute_appex(space, func, *args)
 
     def repr_failure(self, excinfo):
         if excinfo.errisinstance(AppError):
diff --git a/pypy/tool/pytest/test/test_hypothesis.py b/pypy/tool/pytest/test/test_hypothesis.py
new file mode 100644
--- /dev/null
+++ b/pypy/tool/pytest/test/test_hypothesis.py
@@ -0,0 +1,12 @@
+from pypy.tool.pytest.appsupport import app_hypothesis_given
+import hypothesis.strategies as strategies
+
+
+# test for the app-test hypothesis support
+
+ at app_hypothesis_given(strategies.floats(min_value=1.0, max_value=2.0))
+def app_test_floats(f):
+    assert 1.0 <= f <= 2.0
+    assert f == f # not a NaN
+
+


More information about the pypy-commit mailing list