Skip to content

Commit

Permalink
Added Hypothesis support
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed May 7, 2019
1 parent c43afa5 commit 5e4e28d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ It bridges the following functionality:
You can even use it together with native libraries from your selected backend in applications.
Doing this in libraries is not advisable however since it limits the usefulness of your library.

AnyIO comes with its own pytest_ plugin which also supports asynchronous fixtures.
It even works with the popular Hypothesis_ library.

.. _asyncio: https://docs.python.org/3/library/asyncio.html
.. _curio: https://github.com/dabeaz/curio
.. _trio: https://github.com/python-trio/trio
.. _pytest: https://docs.pytest.org/en/latest/
.. _Hypothesis: https://hypothesis.works/
14 changes: 13 additions & 1 deletion anyio/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,21 @@ def pytest_generate_tests(metafunc):

@pytest.mark.tryfirst
def pytest_pyfunc_call(pyfuncitem):
def run_with_hypothesis(**kwargs):
run(partial(original_func, **kwargs), backend=backend)

if pyfuncitem.get_closest_marker('anyio'):
funcargs = pyfuncitem.funcargs
backend = pyfuncitem._request.getfixturevalue('anyio_backend')
if hasattr(pyfuncitem.obj, 'hypothesis'):
# Wrap the inner test function unless it's already wrapped
original_func = pyfuncitem.obj.hypothesis.inner_test
original_func_name = getattr(original_func, '__qualname__', '')
if original_func_name != run_with_hypothesis.__qualname__:
pyfuncitem.obj.hypothesis.inner_test = run_with_hypothesis

return False

funcargs = pyfuncitem.funcargs
testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
run(partial(pyfuncitem.obj, **testargs), backend=backend)
return True
3 changes: 3 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ This library adheres to `Semantic Versioning <http://semver.org/>`_.
cancel scope on asyncio and curio
- Added the ``current_time()`` function
- Replaced ``CancelledError`` with ``get_cancelled_exc_class()``
- Added support for Hypothesis_

.. _Hypothesis: https://hypothesis.works/

**1.0.0rc1**

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ install_requires =
test =
pytest >= 3.7.2
pytest-cov
hypothesis >= 3.64
trio = trio >= 0.11
curio = curio >= 0.9
doc =
Expand Down
9 changes: 9 additions & 0 deletions tests/test_hypothesis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest
from hypothesis import given
from hypothesis.strategies import just


@pytest.mark.anyio
@given(x=just(1))
async def test_hypothesis_wrapper(x):
assert isinstance(x, int)

0 comments on commit 5e4e28d

Please sign in to comment.