python / python/cpython

Alternative to mock_calls for awaits in AsyncMock

Aperta
#121,685 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature or enhancement

Proposal:

I’d like to have an alternative to mock_calls for awaited coroutines to AsyncMock.

Mock.mock_calls contain all calls to the mock and its children. When used on AsyncMock, the mock_calls record is added when coroutine is created, regardless of whether it was awaited. Awaits can be checked using assert_await* methods or await_* attributes, but those only record the mock itself and there is no option to check awaits on the AsyncMock and its children at once.

The best case scenario, it should be possible to switch from sync version

mock = Mock()
mock(sentinel.foo)
assert mock.mock_calls == [call(sentinel.foo)]

to async version

mock = AsyncMock()
await mock(sentinel.foo)
assert mock.mock_awaits == [call(sentinel.foo)]

Since I commonly use mock_calls checks in tests and there is no alternative for awaits, I tend to use it also for async code and omit the checks for awaits. I’m afraid that I may miss some awaits this way.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/alternative-to-mock-calls-for-awaits-in-asyncmock/25425

Guida per i contributori

Apri la guida per i contributori

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.

Direzione di ricerca

Inizia leggendo gli entry point AsyncMock e Mock.mock_calls, insieme ai metodi assert_await* e agli attributi await_* esistenti. Esamina la discussione Discourse collegata per il contesto della progettazione precedente; il lavoro è completo quando esistono un’API concordata e il comportamento corrispondente in grado di verificare gli await su un AsyncMock e sui suoi figli.

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

Valutazione

Stack tecnologico
python
Ambito
testing-qa
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.