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:

36 questions
18
votes
2 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
  • 9,316
  • 4
  • 29
  • 41
11
votes
1 answer

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
  • 680
  • 5
  • 18
9
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
  • 4,433
  • 20
  • 37
8
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
  • 1,817
  • 1
  • 14
  • 24
7
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
  • 7,970
  • 6
  • 34
  • 55
4
votes
0 answers

aiohttp_client - RuntimeError: Timeout context manager should be used inside a task

What I'm Doing I'm learning aiohttp by building a REST api which I'm testing with Pytest (and its async and aiohttp plugins). For my first test (I'm going with TDD from outset) I have the following code: @pytest.mark.asyncio async def…
3
votes
1 answer

How to write pytest fixture for asyncio stream server?

I've been trying to learn asyncio, and I can't find any examples of creating a pytest fixture I can use to test my server code. As soon as the server starts, I guess it blocks everything else, so the tests never run. Does pytest-asyncio have a way…
Mike Conigliaro
  • 970
  • 1
  • 9
  • 22
3
votes
1 answer

For what 'pytest.mark.asyncio' is used?

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…
3
votes
1 answer

Test that a consumer method can raise an exception with Django Channels and pytest-asyncio

Using Django and Channels 2, I have a consumer method that can can be accessed through channel groups and that may raise exceptions. Like this trivial one: from channels.generic.websocket import WebsocketConsumer from asgiref.sync import…
2
votes
0 answers

"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
  • 119
  • 1
  • 11
2
votes
1 answer

Session in an External Transaction with an async engine

I'm trying out a new (beta) 1.4 sqlalchemy and encountered difficulty when trying to port "Joining a Session into an External Transaction (such as for test suite)" recipe using async API and pytest. Firstly, I've tried converting unittest example of…
2
votes
0 answers

Python asynctest mock patch decorator spilling into subsequent tests

I am trying to test an async function. For this i am using pytest-asyncio and asynctest. I need to check how many times a function that is being used inside the function that i am testing is called. For this i am mocking the internal function using…
2
votes
1 answer

Authentication in Django Channels v2 tests with WebSocketCommunicator

In the process of writing tests for my chat consumer I encountered with a problem of being unable to authenticate in tests, using WebSocketCommunicator. I have custom JwtTokenAuthMiddleware that implements authentication in sockets by using token in…
2
votes
2 answers

How do I Mock the coroutine json() when using aiohttp.ClientSession.get

I want to mock the json() coroutine from the aiohttp.ClientSession.get method. It looks to return an async generator object, which is where I'm confused on how to mock in my example. Here is my code: async def get_access_token(): async with…
mroth7684
  • 93
  • 8
2
votes
1 answer

Queue.asyncio ValueError: task_done() called too many times - Coding error or a bug detected?

I implemented a piece of code that get an element from one Queue and put the same object into each queue from a list of queues. The problem is that when I run a specific test I'm getting a ValueError: task_done() called too many times exception.…
Beto
  • 73
  • 7
1
2 3