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
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

Is there an async equivalent of setUpClass in Python 3.10?

I have been using unittest.IsolatedAsyncioTestCase to test my async methods. I have been making use of setUpClass, asyncSetUp to create fixture and asyncTearDown to cleanup. This is all working merrily so far :-) But now I have a new requirement…
vxxxi
  • 155
  • 1
  • 6
2
votes
1 answer

Visual Studio Code debugger does not stop at breakpoints with pytest-asyncio

I have a python project that uses pytest. We have some functions that are async and therefore we need to have async tests. In this sense, we added the pytest-asyncio package as a dependency. Now, we can run our tests, but when we try to debug them…
2
votes
1 answer

Writing pytest testcases for asyncio with streams

I am trying to write a pytest testcase for a asyncio function which does read the output streams (stderr/stdout) and modifies the lines. The function I want to test (which again gets called inside asyncio.gather) is as shown below: import…
SKP
  • 33
  • 6
2
votes
0 answers

How to unit-test asyncio call_later with pytest?

I'm writing some asyncio-based code, and I'm trying to accomplish scheduled actions. I'd like to write some unit-tests for my code. What is the "good" way to unit-test code which involves call_later() actions? I would like to avoid actually waiting…
Niobos
  • 880
  • 4
  • 15
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
  • 8
1
vote
0 answers

Async def functions are not natively supported in pytest for telegram bot python

I am trying to create a pytest file testing multiple functions (start and name) of a telegram bot. I managed to test each function individually and it works, but when I try to test multiple functions, it shows error: PytestUnhandledCoroutineWarning:…
1
vote
0 answers

Get value from sync callback to coroutine few times

I am working now around library for creating testing infrastructure for dbus objects via python-dbus-next library and i have a stuck around assertes for dbus property, which can changed via signal. Typical example of property update its simple sync…
obs
  • 11
  • 2
1
vote
0 answers

I cannot make pytest-bdd work with pytest-asyncio

I am trying to create a test for set some values in a device and then query them from device to assert that they are equal several times with specific time intervals. But for this to work I need to run query and assert things async because I cannot…
1
vote
1 answer

How to test a FastAPI route that retries a SQLAlchemy insert after a rollback?

I have a route where I want to retry an insert if it failed due to an IntegrityError. I’m trying to test it using pytest and httpx but I get an error when I reuse the session to retry the insert after the rollback of the previous one. It works fine…
bfontaine
  • 18,169
  • 13
  • 73
  • 107
1
vote
0 answers

SqlAlchemy select hangs in FastAPI test setup

I developed a FastAPI app with WebSockets and I'm trying to test it. I need async tests in order to check data in the database during tests. My setup looks like this: I have a fixture that generates a database…
1
vote
1 answer

Pytest asyncio - run multiple tests gives error regarding event loop

I am using pytest with pytest-asyncio to run async tests. What is strange is that these tests only work if there is exactly one of them. For example: @pytest.mark.asyncio async def test_foo(): client = await get_db_connection() user = await…
Alex
  • 2,270
  • 3
  • 33
  • 65
1
vote
0 answers

Asyncio PyTest RuntimeError: no running event loop

Hello everyone, I'm writing tests for a game that is written in the python websockets library. When running the test test_game_started an error appears that asyncio loop is not running, but the test passes. I provide the entire test code and the…
1
vote
0 answers

Django Testing Using AsyncClient and Authentication

I have a basic Asynchronous Class Based View: class AsyncAuthenticationView(View): async def post(self, request, *args, **kwargs): authenticated: bool = await sync_to_async(lambda: request.user.is_authenticated)() if not…
garyj
  • 1,302
  • 2
  • 13
  • 22
1
vote
1 answer

pytest-asyncio tests not running async

I'm trying to implement a simple async test suite. If my understanding is correct of async, the tests below should only take about 2 seconds to run. However, it's taking 6 seconds. What am I missing to make these test to run async ("at the same…
efe_man
  • 21
  • 3