You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🐣 Is your feature request related to a problem? Please describe.
As discussed in #4695 python 3.8 (that this library just started supporting) introduced test cases asyncio capable, for versions below 3.8 there is a library asynctest that is almost equal to what has been introduced in python 3.8. This would make the decorator unittest_run_loop useless and make it simpler to write async test cases.
💡 Describe the solution you'd like
As shown in the PR #4697asynctest is useful for people using python below 3.8 for patching but for the testcase and mock as well. I tried to include this library in case python below 3.8 and IsolatedAsyncioTestCase and here is an example of what should change in test_utils.
ifPY_38:
fromunittestimportIsolatedAsyncioTestCaseasTestCaseelse:
fromasynctestimportTestCase# type: ignoreclassAioHTTPTestCase(TestCase):
"""A base class to allow for unittest web applications using aiohttp. Provides the following: * self.client (aiohttp.test_utils.TestClient): an aiohttp test client. * self.loop (asyncio.BaseEventLoop): the event loop in which the application and server are running. * self.app (aiohttp.web.Application): the application returned by self.get_application() Note that the TestClient's methods are asynchronous: you have to execute function on the test client using asynchronous methods. """asyncdefget_application(self) ->Application:
""" This method should be overridden to return the aiohttp.web.Application object to test. """returnself.get_app()
defget_app(self) ->Application:
"""Obsolete method used to constructing web application. Use .get_application() coroutine instead """raiseRuntimeError("Did you forget to define get_application()?")
defsetUp(self) ->None:
ifPY_38:
self.loop=asyncio.get_event_loop()
self.loop.run_until_complete(self.setUpAsync())
asyncdefsetUpAsync(self) ->None:
self.app=awaitself.get_application()
self.server=awaitself.get_server(self.app)
self.client=awaitself.get_client(self.server)
awaitself.client.start_server()
deftearDown(self) ->None:
self.loop.run_until_complete(self.tearDownAsync())
asyncdeftearDownAsync(self) ->None:
awaitself.client.close()
asyncdefget_server(self, app: Application) ->TestServer:
"""Return a TestServer instance."""returnTestServer(app)
asyncdefget_client(self, server: TestServer) ->TestClient:
"""Return a TestClient instance."""returnTestClient(server)
I choose this approach to make it backward compatible, in fact all the tests are passing but I am afraid that make_mocked_coroutine should get the same treatment although I did not change it since I saw that it is widely used in many tests. By the way in asynctest and python3.8 there is AsyncMock that should do the trick.
❓ Describe alternatives you've considered
An alternative would break backwards compatibility, actually setUpAsync and tearDownAsync as modified above could break things as well. Another thing that could break things is that the loop in IsolatedAsyncioTestCase has another name, so I added the check for python version in setUp method.
📋 Additional context
I already have a PR available, with this changes and requirements changes to use asynctest with python below 3.8 only. I still have to take on the docs, I am waiting to see how this discussion goes to start and remove unittest_run_loop decorator
The text was updated successfully, but these errors were encountered:
🐣 Is your feature request related to a problem? Please describe.
As discussed in #4695 python 3.8 (that this library just started supporting) introduced test cases asyncio capable, for versions below 3.8 there is a library
asynctest
that is almost equal to what has been introduced in python 3.8. This would make the decoratorunittest_run_loop
useless and make it simpler to writeasync
test cases.💡 Describe the solution you'd like
As shown in the PR #4697
asynctest
is useful for people using python below 3.8 for patching but for the testcase and mock as well. I tried to include this library in case python below 3.8 andIsolatedAsyncioTestCase
and here is an example of what should change in test_utils.I choose this approach to make it backward compatible, in fact all the tests are passing but I am afraid that make_mocked_coroutine should get the same treatment although I did not change it since I saw that it is widely used in many tests. By the way in
asynctest
and python3.8 there isAsyncMock
that should do the trick.❓ Describe alternatives you've considered
An alternative would break backwards compatibility, actually
setUpAsync
andtearDownAsync
as modified above could break things as well. Another thing that could break things is that the loop inIsolatedAsyncioTestCase
has another name, so I added the check for python version insetUp
method.📋 Additional context
I already have a PR available, with this changes and requirements changes to use
asynctest
with python below 3.8 only. I still have to take on the docs, I am waiting to see how this discussion goes to start and removeunittest_run_loop
decoratorThe text was updated successfully, but these errors were encountered: