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

async fixtures yield async_generator instead of the object yielded #842

Closed
vaharoni opened this issue May 23, 2024 · 1 comment
Closed

Comments

@vaharoni
Copy link

Here's a python test file that I'd expect to pass:

import pytest
from collections.abc import AsyncGenerator

@pytest.fixture()
async def fixture() -> AsyncGenerator[str, None]:
    yield "a"

@pytest.mark.asyncio
async def test(fixture: str):
    assert fixture == "a"

However, it does not pass due to the error: AssertionError: assert <async_generator object fixture at 0x104723740> == 'a'

I'm using:

pytest==8.2.1
pytest-asyncio==0.23.7

Repo to reproduce can be found here.

Full output of pytest -v test.py:

============================================================================== test session starts ==============================================================================
platform darwin -- Python 3.11.2, pytest-8.0.0, pluggy-1.4.0 -- /Users/REDACTED/.pyenv/versions/3.11.2/bin/python3.11
cachedir: .pytest_cache
rootdir: /Users/REDACTED/workspace/pytest-asyncio-issue
plugins: asyncio-0.23.5, xdist-3.5.0, anyio-3.6.2
asyncio: mode=Mode.STRICT
collected 1 item                                                                                                                                                                

test.py::test FAILED                                                                                                                                                      [100%]

=================================================================================== FAILURES ====================================================================================
_____________________________________________________________________________________ test ______________________________________________________________________________________

fixture = <async_generator object fixture at 0x104723740>

    @pytest.mark.asyncio
    async def test(fixture: str):
>       assert fixture == "a"
E       AssertionError: assert <async_generator object fixture at 0x104723740> == 'a'

test.py:10: AssertionError
============================================================================ short test summary info ============================================================================
FAILED test.py::test - AssertionError: assert <async_generator object fixture at 0x104723740> == 'a'
=============================================================================== 1 failed in 0.07s ===============================================================================
@vaharoni
Copy link
Author

Ah, my bad. The correct code should use @pytest_asyncio.fixture rather than @pytest.fixture:

import pytest
import pytest_asyncio
from collections.abc import AsyncGenerator

@pytest_asyncio.fixture()
async def fixture() -> AsyncGenerator[str, None]:
    yield "a"

@pytest.mark.asyncio
async def test(fixture: str):
    assert fixture == "a"

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

No branches or pull requests

1 participant