1

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 problem is that the test freezes, since the event_loop set by pytest-asyncio and mymodule.py event loop are not the same.

This can be fixed by moving import mymodule to inside the test function, but I prefer not to do so.

Is there a way for pytest to patch the default event loop before the test code is imported and executed?

user3599803
  • 6,435
  • 17
  • 69
  • 130
  • did you happen to find a more elegant solution to your problem, @user3599803 ? I thought of trying instead of get_event_loop, that it could be get_running_loop, except for my use case which I'm kind of forced to use py3.6, I'll have to be more creative... – omartin2010 May 20 '20 at 21:43

0 Answers0