Skip to content

Commit

Permalink
fix: Fixed a regression that prevented async fixtures from working in…
Browse files Browse the repository at this point in the history
… sync tests.

The commit simply processes all async fixtures of a test regardless of whether the test is an async function or not.

Closes pytest-dev#286

Signed-off-by: Michael Seifert <[email protected]>
  • Loading branch information
seifertm committed Feb 8, 2022
1 parent 07e9922 commit 8a86c9e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ or an async framework such as `asynctest <https://asynctest.readthedocs.io/en/la
Changelog
---------

0.18.1 (UNRELEASED)
~~~~~~~~~~~~~~~~~~~
- Fixes a regression that prevented async fixtures from working in synchronous tests. `#286 <https://github.com/pytest-dev/pytest-asyncio/issues/286>`_

0.18.0 (22-02-07)
~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ def pytest_pycollect_makeitem(
"""A pytest hook to collect asyncio coroutines."""
if not collector.funcnamefilter(name):
return None
_preprocess_async_fixtures(collector.config, _HOLDER)
if (
_is_coroutine(obj)
or _is_hypothesis_test(obj)
and _hypothesis_test_wraps_coroutine(obj)
):
_preprocess_async_fixtures(collector.config, _HOLDER)
item = pytest.Function.from_parent(collector, name=name)
marker = item.get_closest_marker("asyncio")
if marker is not None:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_asyncio_fixture.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from textwrap import dedent

import pytest

Expand Down Expand Up @@ -39,3 +40,25 @@ async def fixture_with_params(request):
async def test_fixture_with_params(fixture_with_params):
await asyncio.sleep(0)
assert fixture_with_params % 2 == 0


@pytest.mark.parametrize("mode", ("auto", "strict", "legacy"))
def test_sync_function_uses_async_fixture(testdir, mode):
testdir.makepyfile(
dedent(
"""\
import pytest_asyncio
pytest_plugins = 'pytest_asyncio'
@pytest_asyncio.fixture
async def always_true():
return True
def test_sync_function_uses_async_fixture(always_true):
assert always_true is True
"""
)
)
result = testdir.runpytest(f"--asyncio-mode={mode}")
result.assert_outcomes(passed=1)

0 comments on commit 8a86c9e

Please sign in to comment.