Is there a way to fail a test if an explicitly created background task fails after yielding from the fixture that created it?
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 30/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- python
- Ambito
- testing-qa
Direzione di ricerca
Inizia con il fixture asyncio e l'esempio di background-task in questa issue, concentrandoti su come pytest-asyncio gestisce i task creati prima che un fixture restituisca un valore. Verifica se un'eccezione di un task può annullare e far fallire il test invece di lasciare queue.join() bloccato. Il lavoro è completato quando l'errore mostrato viene segnalato da pytest senza richiedere un callback ad hoc.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
I'll start with an example to provide some context:
import asyncio
import contextlib
import itertools
import pytest
class Consumer:
def __init__(self, queue: asyncio.Queue):
self._queue = queue
async def run(self):
for i in itertools.count(0):
item = await self._queue.get()
print(item)
if i == 3:
raise ValueError('oups')
self._queue.task_done()
@pytest.fixture
async def queue() -> asyncio.Queue:
return asyncio.Queue()
@pytest.fixture
async def consumer_service(queue):
consumer = Consumer(queue)
background_task = asyncio.create_task(consumer.run())
yield
background_task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await run_task
@pytest.mark.asyncio
@pytest.mark.usefixtures('consumer_service')
async def test_consumer_consumes(queue: asyncio.Queue):
queue.put_nowait('I')
queue.put_nowait('Will')
queue.put_nowait('Not')
queue.put_nowait('Hang')
await queue.join()
Here, the consumer service fixture will create a background task that runs the consumer's loop. When processing the last item in the queue, a value error is raised and the background task finishes with an exception before marking the last item as done. In this state, the test will hang and there will be no indication that something went astray.
Ideally, there would be some way to react to the background task failing by canceling and failing the test but I have not seen a way to do it. The closest I got to a solution was to attach a done callback to the background task and check for an exception there but I haven't come across an API that would allow me to recover from this or a way to signal the error (without it being captured by pytest).
- Lingua principale
- Python
- Stelle
- 1.7k
- Fork
- 209
- Merge medio
- 4h 52m
- PR unite (30g)
- 14
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pytest-dev/pytest-asyncio
-
Turn deprecation of the "scope" keyword argument of the asyncio marker into an errorForse già presa @mokashang l’ha presa 113 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
pytest-dev/pytest-asyncio#1475 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 56/100
pytest-dev/pytest-asyncio#1514 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Loop factory parametrization tears down async fixtures used by sync testsForse già presa @TLNing260310 l’ha presa 41 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
pytest-dev/pytest-asyncio#1501 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
`pytest_asyncio_loop_factories` hook is not called when `pytest.mark.asyncio` is added via parametrizationForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
pytest-dev/pytest-asyncio#1463 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Don't warn about `asyncio_default_fixture_loop_scope` unless the test suite uses `pytest-asyncio`Forse già presa @DawnofGenX l’ha presa 5 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 52/100
pytest-dev/pytest-asyncio#1344 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di pytest-dev/pytest-asyncio
Issue simili
-
comp/gateway comp/plugins duplicate P3 type/bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
NousResearch/hermes-agent#137019 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
bot-triaged module: docs triaged
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno
-
new-entry
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
BinWang28/audio-ai-hub#194 ·
-
confluence: a paged listing's links re-encode a parameter they carry, where real carries it as sentApertafidelity
Difficoltà 2/5 1-3 ore Idoneità per principianti 63/100
I maintainer di solito rispondono entro 1 giorno