Closed
Description
pytest-asyncio
interprets all async def
based fixtures as things that should be run by the asyncio
event loop. This appears to make it impossible for a test suite to have side-by-side tests for both libraries.
import asyncio
import trio
import pytest
import pytest_trio
@pytest_trio.trio_fixture
async def trio_thing():
await trio.sleep(0)
return 'trio'
@pytest.fixture
async def asyncio_thing():
await asyncio.sleep(0)
return 'asyncio'
@pytest.mark.asyncio
async def test_something_asyncio(asyncio_thing):
await asyncio.sleep(0)
assert asyncio_thing == 'asyncio'
@pytest.mark.trio
async def test_something_trio(trio_thing):
await trio.sleep(0)
assert trio_thing == 'trio'
This results in:
$ pytest test_it.py -s
=========================================================================================== test session starts ===========================================================================================
platform linux -- Python 3.6.5, pytest-4.0.2, py-1.8.0, pluggy-0.9.0 -- /home/piper/python-environments/lahja/bin/python
cachedir: .pytest_cache
rootdir: /home/piper/projects/lahja, inifile: pytest.ini
plugins: xdist-1.25.0, trio-0.5.2, forked-1.0.2, asyncio-0.10.0
collected 2 items
test_it.py::test_something_asyncio PASSED
test_it.py::test_something_trio ERROR
================================================================================================= ERRORS ==================================================================================================
__________________________________________________________________________________ ERROR at setup of test_something_trio __________________________________________________________________________________
@pytest_trio.trio_fixture
async def trio_thing():
> await trio.sleep(0)
test_it.py:11:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../python-environments/lahja/lib/python3.6/site-packages/trio/_timeouts.py:83: in sleep
await _core.checkpoint()
../../../python-environments/lahja/lib/python3.6/site-packages/trio/_core/_run.py:1697: in checkpoint
with CancelScope(deadline=-inf):
../../../python-environments/lahja/lib/python3.6/site-packages/trio/_core/_ki.py:165: in wrapper
return fn(*args, **kwargs)
../../../python-environments/lahja/lib/python3.6/site-packages/trio/_core/_run.py:179: in __enter__
task = _core.current_task()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
def current_task():
"""Return the :class:`Task` object representing the current task.
Returns:
Task: the :class:`Task` that called :func:`current_task`.
"""
try:
return GLOBAL_RUN_CONTEXT.task
except AttributeError:
> raise RuntimeError("must be called from async context") from None
E RuntimeError: must be called from async context
../../../python-environments/lahja/lib/python3.6/site-packages/trio/_core/_run.py:1645: RuntimeError
Metadata
Metadata
Assignees
Labels
No labels