mercredi 26 décembre 2018

How to test aiohttp server?

This is the code I've found in aiohttp docs:

async def test_hello(aiohttp_client):
    app = web.Application()
    app.router.add_get('/', hello)
    client = await aiohttp_client(app)

This uses aiohttp_client pytest fixture. The problem is that it creates a new web.Application(), while my code already have a web.Application(). so Should I import it from the testing modules? Does it mean I need to expose it as a global variable (which I prefer not to)?

My server.py code looks like:

async def init():
    app = web.Application()
    app.add_routes(.. )

loop = asyncio.get_event_loop()
loop.create_task(init())
loop.run_forever()

So my app is not a global variable here.

Second, does it possible, and is it correct, to actually run the server somehow before starting pytest? Because as I see these tests just interact with web.Application() itself, without listening to port and spawning a server process.

Aucun commentaire:

Enregistrer un commentaire