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

unittest support: Coroutine was never awaited

Aperta
#77 19 commenti 2 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
25/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
python
Ambito
testing-qa

Direzione di ricerca

Inizia con il test async contrassegnato con pytest.mark.asyncio e con i fixture data_svc_fixture ed event_loop mostrati nell’issue. Traccia ogni chiamata a get_data ed execute_query_async, quindi esegui il test per identificare perché la coroutine non viene attesa; il lavoro è completato quando l’avviso è scomparso e le asserzioni passano.

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

Descrizione

feature

I am trying to create a test case for the below async function get_data. I get Runtime warning as RuntimeWarning: coroutine 'TestAsyncStatSvc.test_async_get_data' was never awaited testfunction(**testargs)

Below is my code. Please guide on this one. AFAIK, we get this exception when there is no event loop so I created one in the fixture. What is there that I am missing?

async def get_data(self, data_id=None):
          sql = """
              SELECT id, description
              FROM data_table
          """
          sql_params = []
          if data_id:
              sql += """
              WHERE id = $1
              """
              sql_params += [data_id]
          result = await self.dbproxy_async.execute_query_async(sql, sql_params)

          if result.empty and data_id:
              raise NotFound('data %s not found.' % data_id)
          return result.to_json()

Below Is the test case:

  class TestAsyncStatSvc(object):
      
          TEST_DATA = {
                  'Key1': ['Key1', 'Data desc for key1'],
                  'Key2': ['Key2', 'Data desc for key2'],
                  None: [
                      ['Key1', 'Data desc for key1'],
                      ['Key2', 'Data desc for key2']
                  ]
              }
          
          @pytest.mark.asyncio
          async def test_async_get_data(self, data_svc_fixture):
                  for query_value in TESTDATA.keys:

                      execute_stub = MagicMock(return_value=self.TESTDATA[query_value])

                      # Wrap the stub in a coroutine (so it can be awaited)
                      execute_coro = asyncio.coroutine(execute_stub)

                      # Stub the database db_proxy
                      db_proxy = MagicMock()
                      db_proxy.execute_query_async = execute_coro
          
                      result = data_svc_fixture.get_data(data_id=query_value)            
                      assert result == self.TESTDATA[query_value]
                      
                      if query_value is not None:
                          assert len(comm) == 1
                      else:
                          assert len(comm) == 2
          
                  with pytest.raises(NotFound):
                      data_svc_fixture.get_data('Unobtained')

And here are the fixtures:

        class Context:
            def __init__(self, loop, svc_fixture):
                self.loop = loop
                self.svc_fixture = svc_fixture
        
        
        @pytest.yield_fixture(scope="function")
        def data_svc_fixture(db_proxy_fixture, get_service_fixture, my_svc_configurator_fixture, event_loop):
            ctx = Context(event_loop, get_service_fixture('MySvc'))
            yield ctx
            event_loop.run_until_complete(asyncio.gather(*asyncio.Task.all_tasks(event_loop), return_exceptions=True))
    
        @pytest.yield_fixture()
        def event_loop():
              loop = asyncio.new_event_loop()
              asyncio.set_event_loop(loop)
              yield loop
              loop.close()

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.