FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-110774: allow setting the Runner(loop_factory=...) from IsolatedAsyncioTestCase by graingert · Pull Request #110776 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 8 additions & 0 deletions Doc/library/unittest.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,14 @@ Test cases

.. versionadded:: 3.8

.. attribute:: loop_factory

The *loop_factory* passed to :class:`asyncio.Runner`. Override
in subclasses with :class:`asyncio.EventLoop` to avoid using the
asyncio policy system.

Comment thread
graingert marked this conversation as resolved.
.. versionadded:: 3.13
Comment thread
graingert marked this conversation as resolved.

.. coroutinemethod:: asyncSetUp()

Method called to prepare the test fixture. This is called after :meth:`setUp`.
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_unittest/test_async_case.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -484,5 +484,19 @@ async def test_demo1(self):
result = test.run()
self.assertTrue(result.wasSuccessful())

def test_loop_factory(self):
asyncio.set_event_loop_policy(None)

class TestCase1(unittest.IsolatedAsyncioTestCase):
loop_factory = asyncio.EventLoop

async def test_demo1(self):
pass

test = TestCase1('test_demo1')
result = test.run()
self.assertTrue(result.wasSuccessful())
self.assertIsNone(support.maybe_get_event_loop_policy())

if __name__ == "__main__":
unittest.main()
7 changes: 5 additions & 2 deletions Lib/unittest/async_case.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ class IsolatedAsyncioTestCase(TestCase):
# them inside the same task.

# Note: the test case modifies event loop policy if the policy was not instantiated
# yet.
# yet, unless loop_factory=asyncio.EventLoop is set.
# asyncio.get_event_loop_policy() creates a default policy on demand but never
# returns None
# I believe this is not an issue in user level tests but python itself for testing
# should reset a policy in every test module
# by calling asyncio.set_event_loop_policy(None) in tearDownModule()
# or set loop_factory=asyncio.EventLoop

loop_factory = None

def __init__(self, methodName='runTest'):
super().__init__(methodName)
Expand Down Expand Up @@ -118,7 +121,7 @@ def _callMaybeAsync(self, func, /, *args, **kwargs):

def _setupAsyncioRunner(self):
assert self._asyncioRunner is None, 'asyncio runner is already initialized'
runner = asyncio.Runner(debug=True)
runner = asyncio.Runner(debug=True, loop_factory=self.loop_factory)
self._asyncioRunner = runner

def _tearDownAsyncioRunner(self):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support setting the :class:`asyncio.Runner` loop_factory kwarg in :class:`unittest.IsolatedAsyncioTestCase`

Back | FazBrowse Home | New Git URL