[Python-checkins] [3.11] GH-95736: fix IsolatedAsyncioTestCase to initialize Runner bef… (#96042)
pablogsal
webhook-mailer at python.org
Thu Aug 18 09:42:24 EDT 2022
https://github.com/python/cpython/commit/b68ea2a3e4ae6872e5bb69a0deded21f98c1eecc
commit: b68ea2a3e4ae6872e5bb69a0deded21f98c1eecc
branch: 3.11
author: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2022-08-18T14:42:16+01:00
summary:
[3.11] GH-95736: fix IsolatedAsyncioTestCase to initialize Runner bef… (#96042)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>
files:
A Misc/NEWS.d/next/Library/2022-08-11-18-22-29.gh-issue-95736.LzRZXe.rst
M Lib/unittest/async_case.py
M Lib/unittest/test/test_async_case.py
diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py
index a90eed98f871..3457e92e5da2 100644
--- a/Lib/unittest/async_case.py
+++ b/Lib/unittest/async_case.py
@@ -79,6 +79,10 @@ async def enterAsyncContext(self, cm):
return result
def _callSetUp(self):
+ # Force loop to be initialized and set as the current loop
+ # so that setUp functions can use get_event_loop() and get the
+ # correct loop instance.
+ self._asyncioRunner.get_loop()
self._asyncioTestContext.run(self.setUp)
self._callAsync(self.asyncSetUp)
diff --git a/Lib/unittest/test/test_async_case.py b/Lib/unittest/test/test_async_case.py
index beadcac070b4..f59fc760d381 100644
--- a/Lib/unittest/test/test_async_case.py
+++ b/Lib/unittest/test/test_async_case.py
@@ -434,6 +434,21 @@ async def cleanup(self, fut):
test.doCleanups()
self.assertEqual(events, ['asyncSetUp', 'test', 'cleanup'])
+ def test_setup_get_event_loop(self):
+ # See https://github.com/python/cpython/issues/95736
+ # Make sure the default event loop is not used
+ asyncio.set_event_loop(None)
+
+ class TestCase1(unittest.IsolatedAsyncioTestCase):
+ def setUp(self):
+ asyncio.get_event_loop_policy().get_event_loop()
+
+ async def test_demo1(self):
+ pass
+
+ test = TestCase1('test_demo1')
+ result = test.run()
+ self.assertTrue(result.wasSuccessful())
if __name__ == "__main__":
unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2022-08-11-18-22-29.gh-issue-95736.LzRZXe.rst b/Misc/NEWS.d/next/Library/2022-08-11-18-22-29.gh-issue-95736.LzRZXe.rst
new file mode 100644
index 000000000000..abc270fe35ca
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-08-11-18-22-29.gh-issue-95736.LzRZXe.rst
@@ -0,0 +1 @@
+Fix :class:`unittest.IsolatedAsyncioTestCase` to set event loop before calling setup functions. Patch by Kumar Aditya.
More information about the Python-checkins
mailing list