pytest-dev / pytest-asyncio

Asyncio support for pytest
https://pytest-asyncio.readthedocs.io
Apache License 2.0
1.43k stars 152 forks source link

request.getfixturevalue(name) does not work with async fixtures #976

Closed Slarag closed 2 weeks ago

Slarag commented 3 weeks ago

Current Behavior: Calling request.getfixturevalue(name) with the name of an async fixture raises a RuntimeError. Expected Behavior: Calling request.getfixturevalue(name) with the name of an async fixture should initialize the fixture correctly.

Tested on Windows 11 23H2 with Python 3.11

Installed packages

colorama==0.4.6
iniconfig==2.0.0
packaging==24.1
pluggy==1.5.0
pytest==8.3.3
pytest-asyncio==0.24.0

conftest.py

@pytest.fixture(scope="function")
async def fixture1(request):
    return True

tests/test_getfixturevalue_async.py

import pytest

# This works
async def test_direct_fixture(fixture1):
    assert True

# This not
async def test_get_fixturevalue_async(request):
    x = request.getfixturevalue("fixture1")

Traceback

tests\test_getfixturevalue_async.py:7 (test_get_fixturevalue_async)
request = <FixtureRequest for <Coroutine test_get_fixturevalue_async>>

    async def test_get_fixturevalue_async(request):
>       x = request.getfixturevalue("fixture1")

test_getfixturevalue_async.py:9: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\.venv\Lib\site-packages\_pytest\fixtures.py:532: in getfixturevalue
    fixturedef = self._get_active_fixturedef(argname)
..\.venv\Lib\site-packages\_pytest\fixtures.py:617: in _get_active_fixturedef
    fixturedef.execute(request=subrequest)
..\.venv\Lib\site-packages\_pytest\fixtures.py:1091: in execute
    result = ihook.pytest_fixture_setup(fixturedef=self, request=request)
..\.venv\Lib\site-packages\pluggy\_hooks.py:513: in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
..\.venv\Lib\site-packages\pluggy\_manager.py:120: in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
..\.venv\Lib\site-packages\_pytest\setuponly.py:36: in pytest_fixture_setup
    return (yield)
..\.venv\Lib\site-packages\_pytest\fixtures.py:1140: in pytest_fixture_setup
    result = call_fixture_func(fixturefunc, request, kwargs)
..\.venv\Lib\site-packages\_pytest\fixtures.py:898: in call_fixture_func
    fixture_result = fixturefunc(**kwargs)
..\.venv\Lib\site-packages\pytest_asyncio\plugin.py:366: in _async_fixture_wrapper
    return event_loop.run_until_complete(setup())
C:\Python\3.11\Lib\asyncio\base_events.py:630: in run_until_complete
    self._check_running()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <ProactorEventLoop running=False closed=False debug=False>

    def _check_running(self):
        if self.is_running():
>           raise RuntimeError('This event loop is already running')
E           RuntimeError: This event loop is already running

C:\Python\3.11\Lib\asyncio\base_events.py:589: RuntimeError
Slarag commented 3 weeks ago

Duplicate of #112

seifertm commented 2 weeks ago

Even if this is a duplicate, I appreciate your bug report, because it contains a concise reproducer. Thanks @Slarag !