This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: IsolatedAsyncioTestCase closes default event loop
Type: behavior Stage: resolved
Components: asyncio Versions: Python 3.9, Python 3.8
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: arturhoo, asvetlov, yselivanov, zsol
Priority: normal Keywords:

Created on 2020-02-11 19:51 by arturhoo, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg361831 - (view) Author: Artur Rodrigues (arturhoo) Date: 2020-02-11 19:51
This means that subsequent test cases executed within the same application that don't create the event loop will fail. This seems like a behaviour change that wasn't raised on the original PR.

$ cat test.py
from unittest import IsolatedAsyncioTestCase, TestCase, main
import asyncio

class Test1(IsolatedAsyncioTestCase):
    async def test_one(self):
        self.assertTrue(True)

class Test2(TestCase):
    def test_two(self):
        loop = asyncio.get_event_loop()
        self.assertTrue(true)

if __name__ == "__main__":
    main()
$ /usr/local/opt/python@3.8/bin/python3 test.py
.E
======================================================================
ERROR: test_two (__main__.Test2)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 13, in test_two
    loop = asyncio.get_event_loop()
  File "/usr/local/Cellar/python@3.8/3.8.1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/events.py", line 639, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'MainThread'.

----------------------------------------------------------------------
Ran 2 tests in 0.006s

FAILED (errors=1)
$ uname -a
Darwin arturhoo-mbp 18.7.0 Darwin Kernel Version 18.7.0: Sun Dec  1 18:59:03 PST 2019; root:xnu-4903.278.19~1/RELEASE_X86_64 x86_64
msg362593 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-02-24 15:19
The behavior is intentional and follows asyncio.run() semantics.
Moreover, every test should have a separate event loop instance, this is stressed by IsolatedAsyncioTestCase class name
History
Date User Action Args
2022-04-11 14:59:26adminsetgithub: 83794
2020-02-24 15:19:26asvetlovsetstatus: open -> closed
resolution: wont fix
messages: + msg362593

stage: resolved
2020-02-12 20:44:31zsolsetnosy: + zsol
2020-02-11 19:51:37arturhoocreate