Open Andrei-Pozolotin opened 4 years ago
I was facing that one too. It is an incompatibility between pytest-trio and pytest-asyncio. If you have the two installed, then pytest-asyncio will try to run the fixture setup and fail to do so. After uninstalling pytest-asyncio, the test code works as expected.
the following https://pytest-trio.readthedocs.io/en/latest/quickstart.html
test code for
echo_server_handler
import trio import pytest from functools import partial from trio.testing import open_stream_to_socket_listener
The code being tested:
async def echo_server_handler(stream): while True: data = await stream.receive_some(1000) if not data: break await stream.send_all(data)
The fixture:
@pytest.fixture async def echo_client(nursery): listeners = await nursery.start( partial(trio.serve_tcp, echo_server_handler, port=0) ) echo_client = await open_stream_to_socket_listener(listeners[0]) async with echo_client: yield echo_client
A test using the fixture:
async def test_final(echo_client): for test_byte in [b"a", b"b", b"c"]: await echo_client.send_all(test_byte) assert await echo_client.receive_some(1) == test_byte
============================= test session starts ============================== platform linux -- Python 3.8.0, pytest-5.2.2, py-1.8.0, pluggy-0.13.0 rootdir: /home/work/source/git/service, inifile: pytest.ini plugins: hypothesis-4.50.2, trio-0.5.2, asyncio-0.10.0 collected 1 item
service_test/service/a_echo_test.py F
=================================== FAILURES =================================== __ test_final __
async_fn = functools.partial(<function _trio_test_runner_factory.._bootstrap_fixtures_and_run_test at 0x7f2c0d8f2af0>, echo_client=<pytest_trio.plugin.TrioFixture object at 0x7f2c0c562be0>)
clock = SystemClock(offset=46288.026812318036), instruments = []
restrict_keyboard_interrupt_to_checkpoints = False, args = ()
/usr/lib/python3.8/site-packages/trio/_core/_run.py:1790:
runner = Runner(clock=SystemClock(offset=46288.026812318036), instruments=[], io_manager=EpollIOManager(_epoll=<select.epoll ob...d.RLock object owner=0 count=0 at 0x7f2c0d8d76f0>), trio_token=None, ki_pending=False, waiting_for_idle=SortedDict({})) async_fn = functools.partial(<function _trio_test_runner_factory.._bootstrap_fixtures_and_run_test at 0x7f2c0d8f2af0>, echo_client=<pytest_trio.plugin.TrioFixture object at 0x7f2c0c562be0>)
args = ()
/usr/lib/python3.8/site-packages/trio/_core/_run.py:1940:
self = Runner(clock=SystemClock(offset=46288.026812318036), instruments=[], io_manager=EpollIOManager(_epoll=<select.epoll ob...d.RLock object owner=0 count=0 at 0x7f2c0d8d76f0>), trio_token=None, ki_pending=False, waiting_for_idle=SortedDict({})) task = <Task "<fixture 'echo_client'>" at 0x7f2c0d8f7280> outcome = Error(RuntimeError('cannot reuse already awaited coroutine'))
/usr/lib/python3.8/site-packages/trio/_core/_run.py:1410: KeyError
The above exception was the direct cause of the following exception:
async_fn = functools.partial(<function _trio_test_runner_factory.._bootstrap_fixtures_and_run_test at 0x7f2c0d8f2af0>, echo_client=<pytest_trio.plugin.TrioFixture object at 0x7f2c0c562be0>)
clock = SystemClock(offset=46288.026812318036), instruments = []
restrict_keyboard_interrupt_to_checkpoints = False, args = ()
/usr/lib/python3.8/site-packages/trio/_core/_run.py:1794: TrioInternalError ============================== 1 failed in 0.13s =============================== Exception ignored in: <function Nursery.del at 0x7f2c0c94e1f0> Traceback (most recent call last): File "/usr/lib/python3.8/site-packages/trio/_core/_run.py", line 968, in del AssertionError: Exception ignored in: <function Nursery.del at 0x7f2c0c94e1f0> Traceback (most recent call last): File "/usr/lib/python3.8/site-packages/trio/_core/_run.py", line 968, in del AssertionError: