I’m running into an issue when running a unit test. I’ll try my best to explain the problem.
Big picture there are 3 files:
- unit_test.py
- main.py
- helper.py (async code)
When I run the unit test, it calls the run function in main.py, and the code fails on loop.run_until_complete(…) due to a
<code>RuntimeError: Cannot run the event loop while another loop is running
</code>
<code>RuntimeError: Cannot run the event loop while another loop is running
</code>
RuntimeError: Cannot run the event loop while another loop is running
Note: There are other files using main.py that I have no control of and making main.py async would break a lot of things.
unit_test.py
<code> class TestCode(TestCase):
def test_func(self) -> None:
outp = MainClass().run() # runs the function inside main.py
</code>
<code> class TestCode(TestCase):
def test_func(self) -> None:
outp = MainClass().run() # runs the function inside main.py
</code>
class TestCode(TestCase):
def test_func(self) -> None:
outp = MainClass().run() # runs the function inside main.py
main.py
<code>class MainClass():
def run():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
results = loop.run_until_complete(# calls an async function in helper.py)
</code>
<code>class MainClass():
def run():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
results = loop.run_until_complete(# calls an async function in helper.py)
</code>
class MainClass():
def run():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
results = loop.run_until_complete(# calls an async function in helper.py)