2
RuntimeError("Task <Task pending name='Task-5' coro=<test_root() running at /home/david/PycharmProjects/work/tests/main_test.py:12> cb=[_run_until_complete_cb() at /usr/lib/python3.8/asyncio/base_events.py:184]> got Future <Future pending cb=[Protocol._on_waiter_completed()]> attached to a different loop")

How to fix this issue? In docs there a mention of github comment with a solution, but it's outdated and unclear how to apply provided solution to my code (similar to code from docs).

My code:

import pytest
from httpx import AsyncClient
import main  # File with uvicorn starting
from tests import conftest  # File with code from a [link][2]


@pytest.mark.asyncio()
async def test_root():
    async with AsyncClient(app=main.app, base_url="http://test") as ac:
        response = conftest.event_loop(await ac.post("/register", json={
            "phone_number": "+7 931 964 0000",
            "full_name": "Alex Balex"
        }))
    assert response.status_code == 201
salius
  • 918
  • 1
  • 14
  • 30
  • why are you passing in an `event_loop` from `conftest`? the decorator you use should automatically provide an `event_loop` for your coroutine to run – gold_cy Apr 16 '21 at 12:33
  • @gold_cy because it's described in the docs as a solution `If you encounter a RuntimeError: Task attached to a different loop when integrating asynchronous function calls in your tests (e.g. when using MongoDB's MotorClient) check out this issue in the pytest-asyncio repository.` P.S. issue is a comment link in a question. With current code I still getting this error – salius Apr 16 '21 at 13:04
  • what’s the error if you don’t use another event loop and just use the one provided by the decorator – gold_cy Apr 16 '21 at 13:37
  • @gold_cy error in both cases, I will provide proof a little bit later, now my code is under refactoring and invalid – salius Apr 16 '21 at 13:41
  • @gold_cy More verbose is here https://github.com/pytest-dev/pytest-asyncio/issues/207, thanks. – salius Apr 16 '21 at 14:28

0 Answers0