Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression that prevents async fixtures from working in sync tests #287

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)