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

How do I know if a test function is async?

Aperta
#376 7 commenti 0 reazioni 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
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
testing

Direzione di ricerca

Inizia con pytest_generate_tests e il passaggio del README che descrive async_mode=auto e l’inserimento automatico dei marker. Traccia quando viene valutato metafunc.definition.get_closest_marker("asyncio") rispetto al wrapping, quindi verifica il comportamento con un test pytest-asyncio mirato. Il lavoro è completato quando il comportamento documentato dei marker è coerente con quello osservato e il test copre la modalità auto asincrona.

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

Descrizione

feature

Hey there, thanks for the awesome work. :)

I'm trying to check if a function is async or not within pytest_generate_tests. I'm using async_mode=auto and I would like to keep it that way.

My first bet was to simply use inspect.isawaitable(metafunc.function), but I quickly understood that pytest-asyncio wraps my async test functions into synchronous ones, which makes sense.

So the next thing I tried was:

def pytest_generate_tests(metafunc):
    if metafunc.definition.get_closest_marker("asyncio"):
        ...

I would have expected this one to work, because the README says it should:

In auto mode, the pytest.mark.asyncio marker can be omitted, the marker is added automatically to async test functions.

So either there's something obvious I'm missing here, or the README is wrong.

What I'm doing right now (but that's really just a hack):

def pytest_generate_tests(metafunc):
    if inspect.getsource(metafunc.function)[0:5] == "async":
        ...

Is there any better way right now? If not, do you want me to open a PR to automatically mark async test functions with pytest.mark.asyncio when using async_mode=auto?

Cheers!

Lingua principale
Python
Stelle
1.7k
Fork
209
Merge medio
1h 51m
PR unite (30g)
13

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: 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.