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
6
votes
1 answer

Multiple async unit tests fail, but running them one by one will pass

I have two unit tests, if I run them one by one, they pass. If I run them at class level, one pass and the other one fails at response = await ac.post( with the error message: RuntimeError: Event loop is closed @pytest.mark.asyncio async def…
Houman
  • 64,245
  • 87
  • 278
  • 460
6
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…
5
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
  • 1,144
  • 1
  • 13
  • 28
4
votes
2 answers

pytest-asyncio, all test passed but got RuntimeError: Event loop is closed Error

I used pytest-asyncio to test my sqlalchemy[asyncmy] database, All tests passed, but an error was reported. I don't know how this happened. Is it the plugin itself or something I did wrong? ================================ 2 passed in 0.32s…
Oliver Guo
  • 51
  • 3
4
votes
0 answers

Why pytest hangs while testing simple async function?

I have a simple echo server function. Everything works fine if I launch pytest with a single test, but if I launch it with two tests then the second one hangs on waiting for the server to start, and I can't understand why. Here's a complete…
user3447843
  • 123
  • 1
  • 9
4
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…
4
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…
4
votes
0 answers

Why does using pytest-asyncio and @parametrize cause tests to run for longer than without

I have a test. It sends a get request to a list of urls and checks that the response is not 500. @pytest.mark.asyncio @pytest.mark.parametrize('url_test_list', get_all_url_list(HOST_FOR_TEST)) async def test_check_status_urls(self,…
4
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…
3
votes
1 answer

Pytest + SqlAlchemy + FastAPI. Cannot Get data added by sync engine using async engine in tests

TL;DR In my application there are 2 parts. First, part is API which only serves data (has multiple GET endpoints). Second, the event consumer consumes from pubsub and writes to db. I am using sync DB for event consumer and async db for API. My code…
fallen
  • 31
  • 11
3
votes
0 answers

Use the async gcloud.aio.storage to upload blob to new bucket

I'm using gcloud.aio.storage (with the fake-gcs-server emulator) and I've succesfully uploaded blobs to an existing bucket. import os import pytest # Set ENV before Storage import, otherwise it gets set to prod gcs…
3
votes
1 answer

pytest async function as the argument for parameterize

I have an async function which computes a list of urls asynchronously, and I want to use parameterize to generate an async test for each of those urls , which will assert the status code. What I'm trying to do is something like this: import…
3
votes
2 answers

How to use setUp from pytest as an async method?

I have the following code: import asyncio import pytest from mymodule import myasyncfunction from unittest import TestCase class TestDummy(TestCase): def setUp(self): await myasyncfunction() @pytest.mark.asyncio async def…
Damian
  • 1,084
  • 3
  • 14
  • 26
3
votes
2 answers

How can I add the `url_route` key/value into the `scope` for tests?

I'm looking to test my consumer, which uses scope['url_route'] but using HttpCommunicator or ApplicationCommunicator, that param isn't set. How can I set this parameter? The testing documentation is very limited and doesn't have any documentation on…
notorious.no
  • 4,919
  • 3
  • 20
  • 34
3
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
  • 103
  • 8