diff options
author | Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> | 2022-08-18 13:42:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-18 13:42:16 (GMT) |
commit | b68ea2a3e4ae6872e5bb69a0deded21f98c1eecc (patch) | |
tree | c7d1544e654ed26a516e0c832d66c34559903292 /Lib/unittest | |
parent | 1b9b4856c86f3895445d8bd57a7ab18fc5cc9b20 (diff) | |
download | cpython-b68ea2a3e4ae6872e5bb69a0deded21f98c1eecc.zip cpython-b68ea2a3e4ae6872e5bb69a0deded21f98c1eecc.tar.gz cpython-b68ea2a3e4ae6872e5bb69a0deded21f98c1eecc.tar.bz2 |
[3.11] GH-95736: fix IsolatedAsyncioTestCase to initialize Runner bef… (#96042)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/async_case.py | 4 | ||||
-rw-r--r-- | Lib/unittest/test/test_async_case.py | 15 |
2 files changed, 19 insertions, 0 deletions
diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py index a90eed9..3457e92 100644 --- a/Lib/unittest/async_case.py +++ b/Lib/unittest/async_case.py @@ -79,6 +79,10 @@ class IsolatedAsyncioTestCase(TestCase): 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 beadcac..f59fc76 100644 --- a/Lib/unittest/test/test_async_case.py +++ b/Lib/unittest/test/test_async_case.py @@ -434,6 +434,21 @@ class TestAsyncCase(unittest.TestCase): 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() |