Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support async tests which use Hypothesis #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ dist
doc/_build
virtualenv/
venv*/
.hypothesis/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ matrix:

install:
- curl https://bootstrap.pypa.io/get-pip.py | python
- python -m pip install --upgrade wheel>=0.30.0 setuptools>=36.6.0
- python -m pip install --upgrade wheel>=0.30.0 setuptools>=36.6.0 hypothesis>=3.64

script:
- python -m unittest --verbose test
Expand Down
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Pyotr Ermishkin <[email protected]>
Random User <[email protected]>
Rémi Cardona <[email protected]>
Sviatoslav Sydorenko <[email protected]>
Stefan Tjarks <[email protected]>
29 changes: 29 additions & 0 deletions asynctest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ def debug(self):
self._unset_loop()

def _run_test_method(self, method):
# If the coroutine is decorated by hypothesis wrap
# it before running.
if getattr(method, "is_hypothesis_test", False) and asyncio.iscoroutinefunction(
method.hypothesis.inner_test
):
method.hypothesis.inner_test = wrap_in_sync(method.hypothesis.inner_test)
Copy link
Contributor

@Kentzo Kentzo Dec 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's is the type of method when is_hypothesis_test is true? Is it something that any of the inspect.iscoroutinefunction / inspect.iscoroutine / inspect.isawaitable functions recognize?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just a normal function/callable. Hypothesis given decorator will "swallow" the coroutine and replace it with a normal function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what would be the result of method() then? A coroutine / awaitable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I recall correct it will be None and a python warning when debug is true.


# If the method is a coroutine or returns a coroutine, run it on the
# loop
result = method()
Expand Down Expand Up @@ -508,3 +515,25 @@ def ignore_loop(func=None):
"fail_on(unused_loop=False)", DeprecationWarning)
checker = asynctest._fail_on._fail_on({"unused_loop": False})
return checker if func is None else checker(func)


def wrap_in_sync(func):
"""Return a sync wrapper around an async function executing it in the
current event loop.
"""

@functools.wraps(func)
def inner(**kwargs):
coro = func(**kwargs)
if coro is not None:
task = asyncio.ensure_future(coro)
try:
asyncio.get_event_loop().run_until_complete(task)
except BaseException:
# run_until_complete doesn't get the result from exceptions
# that are not subclasses of `Exception`. Consume all
# exceptions to prevent asyncio's warning from logging.
if task.done() and not task.cancelled():
task.exception()
raise
return inner
15 changes: 15 additions & 0 deletions test/test_hypothesis_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Tests for the Hypothesis integration, which wraps async functions in a
sync shim for Hypothesis.
"""

import asynctest

from hypothesis import given, strategies as st


class TestHypothesisIntegration(asynctest.TestCase):

@given(st.integers())
async def test_mark_inner(self, n):
assert isinstance(n, int)