Skip to content

Commit d337770

Browse files
committed
refac: move hypothesis code into integration/
1 parent 9be2729 commit d337770

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed

pytest_asyncio_cooperative/integration/__init__.py

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import asyncio
2+
import functools
3+
import time
4+
5+
from ..fixtures import fill_fixtures
6+
7+
8+
async def hypothesis_test_wrapper(item):
9+
"""
10+
Hypothesis is synchronous, let's run inside an executor to keep asynchronicity
11+
"""
12+
13+
# Do setup
14+
item.start_setup = time.time()
15+
fixture_values, teardowns = await fill_fixtures(item)
16+
item.stop_setup = time.time()
17+
18+
default_loop = asyncio.get_running_loop()
19+
inner_test = item.function.hypothesis.inner_test
20+
21+
def async_to_sync(*args, **kwargs):
22+
# FIXME: can we cache this loop across multiple runs?
23+
loop = asyncio.new_event_loop()
24+
task = inner_test(*args, **kwargs)
25+
try:
26+
loop.run_until_complete(task)
27+
finally:
28+
loop.close()
29+
30+
# Run test
31+
item.function.hypothesis.inner_test = async_to_sync
32+
wrapped_func_with_fixtures = functools.partial(item.function, *fixture_values)
33+
await default_loop.run_in_executor(None, wrapped_func_with_fixtures)
34+
35+
# Do teardowns
36+
item.start_teardown = time.time()
37+
for teardown in teardowns:
38+
try:
39+
await teardown.__anext__()
40+
except StopAsyncIteration:
41+
pass
42+
item.stop_teardown = time.time()

pytest_asyncio_cooperative/plugin.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import collections.abc
3-
import functools
43
import inspect
54
import threading
65
import time
@@ -12,6 +11,7 @@
1211

1312
from .assertion import activate_assert_rewrite
1413
from .fixtures import fill_fixtures
14+
from .integration.hypothesis import hypothesis_test_wrapper
1515

1616

1717
def pytest_addoption(parser):
@@ -115,44 +115,6 @@ async def do_teardowns():
115115
await do_teardowns()
116116

117117

118-
# TODO: move to hypothesis module
119-
async def hypothesis_test_wrapper(item):
120-
"""
121-
Hypothesis is synchronous, let's run inside an executor to keep asynchronicity
122-
"""
123-
124-
# Do setup
125-
item.start_setup = time.time()
126-
fixture_values, teardowns = await fill_fixtures(item)
127-
item.stop_setup = time.time()
128-
129-
default_loop = asyncio.get_running_loop()
130-
inner_test = item.function.hypothesis.inner_test
131-
132-
def async_to_sync(*args, **kwargs):
133-
# FIXME: can we cache this loop across multiple runs?
134-
loop = asyncio.new_event_loop()
135-
task = inner_test(*args, **kwargs)
136-
try:
137-
loop.run_until_complete(task)
138-
finally:
139-
loop.close()
140-
141-
# Run test
142-
item.function.hypothesis.inner_test = async_to_sync
143-
wrapped_func_with_fixtures = functools.partial(item.function, *fixture_values)
144-
await default_loop.run_in_executor(None, wrapped_func_with_fixtures)
145-
146-
# Do teardowns
147-
item.start_teardown = time.time()
148-
for teardown in teardowns:
149-
try:
150-
await teardown.__anext__()
151-
except StopAsyncIteration:
152-
pass
153-
item.stop_teardown = time.time()
154-
155-
156118
def item_to_task(item):
157119
if getattr(item.function, "is_hypothesis_test", False):
158120
return hypothesis_test_wrapper(item)

0 commit comments

Comments
 (0)