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

pytest-anyio should raise error if sync test relies on async fixture #789

Open
1 task done
jakkdl opened this issue Sep 17, 2024 · 2 comments · May be fixed by pytest-dev/pytest#12930
Open
1 task done

pytest-anyio should raise error if sync test relies on async fixture #789

jakkdl opened this issue Sep 17, 2024 · 2 comments · May be fixed by pytest-dev/pytest#12930
Labels
enhancement New feature or request

Comments

@jakkdl
Copy link

jakkdl commented Sep 17, 2024

Things to check first

  • I have searched the existing issues and didn't find my feature already requested there

Feature description

Sync test function attempting to use async fixture fails silently. pytest-trio will raise a RuntimeError in this instance.

The one possible downside is that this will break users who have async autouse fixtures in the same file where they have sync tests. But this restriction probably isn't too onerous, and may catch bugs as well.

Use case

Catch bugs and align behavior with pytest-trio

import pytest

pytestmark = pytest.mark.anyio

@pytest.fixture(autouse=True)
async def async_fixture():
    assert False
    yield


def test_1(async_fixture):
    assert True

def test_2():
    assert True

will pass, but probably shouldn't

@euri10
Copy link

euri10 commented Oct 7, 2024

big +1 on this

@jakkdl
Copy link
Author

jakkdl commented Nov 1, 2024

It appears this works if the sync test explicitly requests the anyio_backend fixture

import pytest

@pytest.fixture
async def fix():
    return 1

def test_foo(fix, anyio_backend):
    assert fix == 1

but if using @pytest.mark.anyio, pytestmark = pytest.mark.anyio, or forgetting to add anything, you get an unawaited coroutine object as the value.

EDIT:
ah, it's about the fixture requesting (directly, or indirectly from the test requesting it) anyio_backend, so this also works:

import pytest

@pytest.fixture
async def fix(anyio_backend):
    return 1

def test_foo(fix):
    assert fix == 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants