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 AioHTTPTestCase
from aiohttp import web
class MyAppTestCase(AioHTTPTestCase):
async def get_application(self):
"""
Override the get_app method to return your application.
"""
async def hello(request):
return web.Response(text='Hello, world')
app = web.Application()
app.router.add_get('/', hello) # relative URL
return app
async def test_example(self):
async with self.client.request("GET", "/") as resp:
self.assertEqual(resp.status, 200)
text = await resp.text()
self.assertIn("Hello, world", text)
Unlike In the given example, I would like to use ClientSession
:
async with self.client.session as sess:
resp = await sess.get('/') # here, I'd like to use an absolute URL
ClientSession
requires an absolute URL, see https://github.com/aio-libs/aiohttp/blob/ed196893b6102cedcc98251c0c76d1b9c5103cee/aiohttp/test_utils.py#L298-L302.
I looked at aiohttp unittest with URLs not GET but I do not understand how the approach with the responses
lib would fit into my setup. I got
TypeError: The first argument should be web.Application instance, got None