Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

"The test […] is not an async function" when `asyncio` marker is added via `pytest_collection_modifyitems()`

Aperta
#810 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
python
Ambito
testing-qa

Direzione di ricerca

Inizia con il riproduttore in conftest.py e test_foo.py, poi esamina come pytest-asyncio gestisce i marker aggiunti da pytest_collection_modifyitems(). Confronta il comportamento tra pytest-asyncio 0.23.6 e 0.21.1. Il lavoro è completato quando il test async viene riconosciuto ed eseguito quando il marker asyncio viene aggiunto durante la raccolta, senza l’avviso mostrato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

This one I've noticed while testing IPython. The simplest reproducer is:

conftest.py:

import inspect


def pytest_collection_modifyitems(items):
    for item in items:
        if inspect.iscoroutinefunction(item.obj):
            item.add_marker("asyncio")

test_foo.py:

async def test_foo():
    pass

With pytest-asyncio 0.23.6, I'm getting:

========================================================= test session starts =========================================================
platform linux -- Python 3.11.8, pytest-8.1.1, pluggy-1.4.0
rootdir: /tmp/repro
plugins: asyncio-0.23.6
asyncio: mode=Mode.STRICT
collected 1 item                                                                                                                      

test_foo.py s                                                                                                                   [100%]

========================================================== warnings summary ===========================================================
test_foo.py::test_foo
  test_foo.py:5: PytestWarning: The test <Function test_foo> is marked with '@pytest.mark.asyncio' but it is not an async function. Please remove the asyncio mark. If the test is not marked explicitly, check for global marks applied via 'pytestmark'.
    async def test_foo():

test_foo.py::test_foo
  /tmp/repro/.venv/lib/python3.11/site-packages/_pytest/python.py:184: 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, 2 warnings in 0.01s ====================================================

With pytest-asyncio-0.21.1, the test is correctly run as marked.

Lingua principale
Python
Stelle
1.7k
Fork
207
Merge medio
5h 2m
PR unite (30g)
11

Preparare l'ambiente

Non abbiamo ancora controllato i file di configurazione di questo progetto. Parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pytest-dev/pytest-asyncio

Tutte le issue di pytest-dev/pytest-asyncio

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.