I am new using asyncio, I have a test function that I am trying to test using pytest: This is my test structure:
MyClass()
async def myFunction(payload, headers):
session = aiohttp.ClientSession()
with session.post(url, json=payload, headers=headers) as resp:
response = await resp.json()
return response
then I have my test like:
def my_test():
loop = asyncio.get_event_loop()
reponse = loop.run_until_complete(MyClass().myFunction(url, headers))
loop.close()
when I run my tests I got:
pytest runtimeerror there is no current event loop in thread 'mainthread'
How can I fix it, I feel I need to handle the thread in a different way.
I have seen some related questions but no one using pytests Finally I am using Python 3.8.6.