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
1
vote
2 answers

pytest-asyncio RuntimeError: Cannot run the event loop while another loop is running

When trying to do UI automtion with pytest-asyncio and pytest-playwright, I got exception like: RuntimeError: Cannot run the event loop while another loop is running Code structure: ui2/conftest.py ui2/test_bing.py ui2/conftest.py import…
Silence He
  • 111
  • 1
  • 4
1
vote
1 answer

How do I add an attribute to AsyncMock?

Code: class SomeClass(BaseClass): async def async_method(arg1, arg2, **kwargs): await self.foo.bar(arg1=arg1, arg2=arg2).baz(**kwargs) One of the tests used: @pytest.fixture def same_class(): return SameClass() async def…
Maksim
  • 11
  • 2
1
vote
1 answer

How to call an async function in pytest_sessionfinish()?

I am using pytest-asyncio. I have the following conftest.py file: import asyncio import pytest from database.mongo_db import mongo @pytest.fixture(scope="session", autouse=True) async def initialise_db(): await mongo.connect_client() …
KOB
  • 4,084
  • 9
  • 44
  • 88
1
vote
0 answers

'NoneType' object has no attribute 'get_collection', FastAPI + Mongodb

I am doing a test with FastAPI about the register of one user in the app. While app is running i can register a user perfectly but when i try to make a test with pytest about the same method i have problems, it doesn't recognise the…
Cristoff
  • 149
  • 6
1
vote
0 answers

Cant raise error in a async function test

Im using pytest-asyncio in a project that I'm currently working on. In this project Im implementing the repository pattern and for tests I code a simple "In memory repository" (ie: dict with pk on keys and entities on values ). This repository is a…
Tâmer Cuba
  • 2,373
  • 2
  • 11
  • 26
1
vote
0 answers

How to test asyncio streams (coroutines, tasks)?

What should I do to test the login coroutine? class Client: def __init__(self, config=None): self.config = config or ('0.0.0.0', 8080) self.reader = None self.writer = None self.connection =…
1
vote
0 answers

pytest mark asyncio patch default event loop on startup

I have my_module.py that I want to test. In the module top level code, I have: import asyncio loop = asyncio.get_event_loop() # code... test1.py: import mymodule @pytest.mark.asyncio def test_mymodule(event_loop): await mymodule.func() The…
user3599803
  • 6,435
  • 17
  • 69
  • 130
1
vote
0 answers

pytest-asyncio monkey patch requests.post does not work

I am trying to do monkeypatch to the requests.post inside test_app and i am using the test_client from quart(rest-api) which is async . and the object that i get back from the response is good but the test collapse import my_app import…
1
vote
0 answers

python3 asyncio and post request

Using Python3.7, I have two py scripts. server_execute.py is to accept a post request, while once accepted, it is to trigger the server_scripts.py which is to kick off test script executions asynchronously. The server_execute.py script will accept a…
Jaime
  • 11
  • 1
1
vote
1 answer

How to test that asyncio.Queue did NOT get something pushed

I'm currently writing some async tests with pytest and found myself running into the following situation. Consider we have an asyncio.Queue called peer2_subscriber that we want to check if it received a certain message (after triggering some action,…
Christoph
  • 26,519
  • 28
  • 95
  • 133
0
votes
0 answers

How to share data initialised by a function-scoped fixture across multiple test cases

I have a fixture that initializes the database, and the process is quite expensive (create tables, insert some data, etc). The fixture is function-scoped, since normally tests mutate the state somehow, and it's not safe to share. However, now I have…
0
votes
0 answers

asyncio subprocess with pytest does not call pipe_data_received

I'd like to use asyncio subprocess with pytest-asyncio and it looks like it doesn't work as expected, my code base on https://docs.python.org/3.11/library/asyncio-protocol.html#loop-subprocess-exec-and-subprocessprotocol Server is working fine on…
0
votes
1 answer

"Runtime Error: Event loop is closed" during testing using Pytest

Please help me. How to fix it? The error appears only when using cookies in test_logout() conftest.py ............... @pytest_asyncio.fixture(autouse=True, scope='session') async def prepare_database(): async with engine_test.begin() as conn: …
0
votes
2 answers

How to test an async function and patch a class called within

I have the following directory structure: ├── project │ ├── modules │ │ ├── __init__.py.py │ │ ├── module.py │ ├── main.py ├── tests │ ├── test_main.py where module.py is: class My_Class(object): def __init__(self): raise…
0
votes
1 answer

What causes error 'async_generator' object has no attribute 'add'?

I'm writing pytest tests for my FastAPI async project. Tests causes error : session.add(user) AttributeError: 'async_generator' object has no attribute 'add' conftest.py: SQLALCHEMY_DATABASE_URL =…