mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
GH-95736: fix IsolatedAsyncioTestCase to initialize Runner before calling setup functions (#95898)
This commit is contained in:
parent
5a8c15819c
commit
9d515997f9
3 changed files with 20 additions and 0 deletions
|
@ -434,6 +434,21 @@ class TestAsyncCase(unittest.TestCase):
|
||||||
test.doCleanups()
|
test.doCleanups()
|
||||||
self.assertEqual(events, ['asyncSetUp', 'test', 'cleanup'])
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -116,6 +116,10 @@ class IsolatedAsyncioTestCase(TestCase):
|
||||||
assert self._asyncioRunner is None, 'asyncio runner is already initialized'
|
assert self._asyncioRunner is None, 'asyncio runner is already initialized'
|
||||||
runner = asyncio.Runner(debug=True)
|
runner = asyncio.Runner(debug=True)
|
||||||
self._asyncioRunner = runner
|
self._asyncioRunner = runner
|
||||||
|
# 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.
|
||||||
|
runner.get_loop()
|
||||||
|
|
||||||
def _tearDownAsyncioRunner(self):
|
def _tearDownAsyncioRunner(self):
|
||||||
runner = self._asyncioRunner
|
runner = self._asyncioRunner
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix :class:`unittest.IsolatedAsyncioTestCase` to set event loop before calling setup functions. Patch by Kumar Aditya.
|
Loading…
Add table
Add a link
Reference in a new issue