Questions tagged [pytest-aiohttp]
26 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
25
votes
2 answers
How can I mock out responses made by aiohttp.ClientSession?
I am using aiohttp to make asynchronous requests and I want to test my code. I want to mock out requests sent by aiohttp.ClientSession. I am looking for something similar to the way responses handles mocking for the requests lib.
How can I mock out…

mcranston18
- 4,680
- 3
- 36
- 37
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…

mfonism
- 535
- 6
- 15
3
votes
0 answers
python asyncio - ConnectionResetError on one async server closes also second server
I have a question regarding two async servers that run on the same event loop. when I close one connection from client side, I see that the second server stops as well.
Here is my code:
async def http_server(addr: str, port: int, verbose: int):
…

Yaron
- 31
- 1
3
votes
1 answer
Sending multipart request by aiohttp
I trying to make test case for my view, and now I would to send mutipart request with user credentials containing user image. I use MultipartWriter but, when I'm trying to read parts, I get part as None. I'm sure that my test case is wrong…

Артём Котков
- 147
- 1
- 8
3
votes
0 answers
Testing aiohttp Server With Asynchronous Fixtures
I'm trying to using pytest-aiohttp to test my aiohttp based REST API. There is an /authenticate endpoint which returns an authentication token and essentially all other endpoints require the token in the Authorization header. I have the following…

tedm1106
- 127
- 1
- 10
2
votes
1 answer
aiohttp Mock ClientSession Response
Trying to mock the response of a aiohttp.ClientSession for testing purposes
My code looks like this:
async def run_request(endpoint: str, **kwargs) -> dict:
async with aiohttp.ClientSession() as session:
async with session.post(endpoint,…

Jonathan Mor
- 51
- 6
2
votes
1 answer
Aiohttp async session requests
So i've been scraping a website (www.cardsphere.com) protected pages with requests, using session, like so:
import requests
payload = {
'email': ,
'password':
}
with…

Last_crusaider
- 33
- 1
- 1
- 4
2
votes
2 answers
AIOHTTP:TypeError: index() takes 0 positional arguments but 1 was given
from aiohttp import web
import aiohttp
from settings import config
import asyncio
import psycopg2 as p
import json
import aiopg
import aiohttp
import asyncio
async def fetch(client):
async with…

john
- 539
- 2
- 9
- 24
1
vote
1 answer
Python Aiohttp: cookies behaviour on same domain
Hopefully this is not a too stupid question, but I am having trouble with aiohttp cookie processing.
Aiohttp's CookieJar class mentions it implements cookie storage adhering to RFC 6265, which states that:
cookies for a given host are shared across…

Clément de Nailly
- 210
- 4
- 8
1
vote
0 answers
aiohttp unittest: Client Session Request Requires Absolute URL
I am trying to write a test for a method that uses aiohttp's ClientSession.
I am using Python's unittest framework.
I followed the approach described here: https://docs.aiohttp.org/en/stable/testing.html#unittest:
from aiohttp.test_utils import…

user17616096
- 11
- 2
1
vote
1 answer
Python aiohttp how to handle client session token timeout
I am making several 100's of http request using aiohttp. I am relatively new to the async world but have managed to get the basic code working.
First I am generating a token. Then, making aiohttp calls using this token.
Token has a validity of 30…

CD.
- 43
- 3
1
vote
1 answer
Wrong simple async script (python 3.5)
I have such a simple code.
from aiohttp import web
async def hello(request):
print('Start')
for el in range(30000000):
# Any expression
1+el/10000*100000-4*234
print('Stop')
return web.Response(text="Hello,…

guri
- 11
- 1
0
votes
1 answer
How to mock the asyncio.run method?
I need to mock a result of an async function which uses inside another function calling asyncio.run(func(*args,**kwargs))
Example code
async def get_responses(
**kwargs
) -> list:
start_time = time.monotonic()
responses: list = []
…

HashNoxt
- 48
- 5
0
votes
1 answer
How to handle AioHttpClient in Pytest?
I am writing my first test (unit test with Pytest), that contains AioHttpClient with BasicAuth (with username and poassword).
My function's structure:
async def example_function(some parameters):
(...)
try:
synth_url =…

Mr.Slow
- 490
- 1
- 1
- 16