Questions tagged [pytest-asyncio]

pytest-asyncio is a pytest plugin that facilitates testing asyncio code by providing helper fixtures and markers.

pytest-asyncio is a pytest plugin that provides several fixtures and markers that help testing asyncio code more easily. These help for example to test asyncio event loops, or to use test functions as asyncio coroutines.

Reference:

82 questions
46
votes
3 answers

Async fixtures with pytest

How do I define async fixtures and use them in async tests? The following code, all in the same file, fails miserably. Is the fixture called plainly by the test runner and not awaited? @pytest.fixture async def create_x(api_client): x_id = await…
Pynchia
  • 10,996
  • 5
  • 34
  • 43
26
votes
1 answer

Pytest skips test saying "asyncio not installed"

When testing the following code @pytest.mark.asynico async def test_handle_DATA(mocker): handle_mock = mocker.MagicMock() envelope_mock = mocker.MagicMock(mail_from="Test@From", rcpt_tos=["Test@To"], content=b"TestContent") result =…
Daniel Butler
  • 3,239
  • 2
  • 24
  • 37
23
votes
3 answers

How to test async function using pytest?

@pytest.fixture def d_service(): c = DService() return c # @pytest.mark.asyncio # tried it too async def test_get_file_list(d_service): files = await d_service.get_file_list('') print(files) However, it got the following…
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
14
votes
2 answers

pytest-asyncio has a closed event loop, but only when running all tests

I have a test to verify an exception is thrown from an async response, which I'm using pytest-asyncio version 0.10.0 to run. Code is basically: class TestThis: @pytest.mark.asyncio def test_the_thing(self): arg1 = "cmd" arg2 =…
Mathieson
  • 1,698
  • 2
  • 17
  • 19
14
votes
4 answers

How to timeout an async test in pytest with fixture?

I am testing an async function that might get deadlocked. I tried to add a fixture to limit the function to only run for 5 seconds before raising a failure, but it hasn't worked so far. Setup: pipenv --python==3.6 pipenv install pytest==4.4.1 pipenv…
T Tse
  • 786
  • 7
  • 19
13
votes
2 answers

Using @pytest.fixture(scope="module") with @pytest.mark.asyncio

I think the example below is a really common use case: create a connection to a database once, pass this connection around to test which insert data pass the connection to a test which verifies the data. Changing the scope of…
Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62
11
votes
2 answers

What is 'pytest.mark.asyncio' is used for?

I don't understand for which purposes the decorator @pytest.mark.asyncio can be used. I've tried to run the following code snippet with pytest and pytest-asyncio plugin installed and it failed, so I concluded that pytest collects test coroutines…
Egor Osokin
  • 113
  • 1
  • 1
  • 6
11
votes
3 answers

Run tests concurrently

I would like to run several tests concurrently using asyncio (/curio/trio) and pytest, but I couldn't find any information on that. Do I need to schedule them myself? And if I do, is there a way to have a nice output that separates (sub-)tests…
cglacet
  • 8,873
  • 4
  • 45
  • 60
10
votes
2 answers

FastAPI, SQLAlchemy, pytest, unable to get 100% coverage, it doesn't properly collected

I'm trying to build FastAPI application fully covered with test using python 3.9 For this purpose I've chosen stack: FastAPI, uvicorn, SQLAlchemy, asyncpg, pytest (+ async, cov plugins), coverage and httpx AsyncClient Here is my minimal…
9
votes
0 answers

How do you use httpx AsyncClient with pytest in fastapi?

I have tried everything I've been able to find, but I can't get async tests working. I get RuntimeError: This event loop is already running running TestClient (which makes sense based on the docs), but I get httpx.ConnectError: [Errno 8] nodename…
Marc LaBelle
  • 270
  • 5
  • 12
8
votes
0 answers

Pytest asyncio event is bound to a different event loop, event loop is closed

I am trying to write some tests for my fastapi app I am using the prisma-client-py for the database. I don't know if that changes anything Everything works as they are supposed to apart from every first and last, they both fail with the same…
7
votes
1 answer

"Exception Ignored: RuntimeError: Event loop is closed" when using pytest-asyncio and aiohttp

I'm writing python tests with pytest and have some async code I want to test so I installed the pytest-asyncio plugin. The async code uses aiohttp and running the tests I get the following warning/error/hint AFTER the test runs…
hadamard
  • 401
  • 4
  • 16
7
votes
1 answer

pytest-asyncio with singletons causes conflicting event loops

I'm using dbus for IPC. In order to have exactly one bus throughout the livetime of my program I'm using a singleton here. For the sake of demonstration I'm connecting to NetworkManager but that could be exchanged. Furthermore, I'm using asyncio for…
grmmgrmm
  • 994
  • 10
  • 29
6
votes
0 answers

Pytest returning event loop closed (FastAPI)

Playing with a small FastAPI application but getting 'event loop closed' RuntimeError when running pytest against my endpoints. I have some database operations like this using AsyncIOMotorClient: async def get_user_data(token: str, client =…
Dave
  • 61
  • 4
6
votes
2 answers

"RuntimeError: Event loop is closed" when using pytest-asyncio to test FastAPI routes

I received the error RuntimeError: Event loop is closed each time I try to make more than one async call inside my test. I already tried to use all other suggestions from other Stack Overflow posts to rewrite the event_loop fixture but nothing…
Dmitriy_kzn
  • 518
  • 4
  • 16
1
2 3 4 5 6