Skip to content

Inconsistent behavior for sync and async generator functions #642

Closed
@seifertm

Description

@seifertm

Pytest no longer supports tests based generator functions and will xfail the respective test:

test_generator.py

def test_gen():
    yield
$ pytest test_generator.py 
============================================================================================================================ test session starts =============================================================================================================================
platform linux -- Python 3.11.6, pytest-7.4.2, pluggy-1.3.0
rootdir: /tmp/tst
plugins: asyncio-0.21.1
asyncio: mode=Mode.STRICT
collected 1 item                                                                                                                                                                                                                                                             

test_generator.py x                                                                                                                                                                                                                                                    [100%]

============================================================================================================================== warnings summary ==============================================================================================================================
test_generator.py:1
  test_generator.py:1: PytestCollectionWarning: yield tests were removed in pytest 4.0 - test_gen will be ignored
    def test_gen():

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
======================================================================================================================= 1 xfailed, 1 warning in 0.15s ========================================================================================================================

Pytest-asyncio v0.21.1 simply refuses to take care of async generator functions, which causes pytest to complain about a missing plugin for async tests.

test_a.py

async def test_async_gen():
    yield
$ pytest --asyncio-mode=auto test_a.py
============================================================================================================================ test session starts =============================================================================================================================
platform linux -- Python 3.11.6, pytest-7.4.2, pluggy-1.3.0
rootdir: /tmp/tst
plugins: asyncio-0.21.1
asyncio: mode=Mode.AUTO
collected 1 item                                                                                                                                                                                                                                                             

test_a.py s                                                                                                                                                                                                                                                            [100%]

============================================================================================================================== warnings summary ==============================================================================================================================
test_a.py::test_async_gen
  /tmp/tst/venv/lib/python3.11/site-packages/_pytest/python.py:183: PytestUnhandledCoroutineWarning: async def functions are not natively supported and have been skipped.
  You need to install a suitable plugin for your async framework, for example:
    - anyio
    - pytest-asyncio
    - pytest-tornasync
    - pytest-trio
    - pytest-twisted
    warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid)))

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
======================================================================================================================= 1 skipped, 1 warning in 0.03s ========================================================================================================================

Pytest-asyncio should handle tests based on asynchronous generator functions and raise a warning similar to pytest.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions