diff --git a/CHANGES/5516.misc b/CHANGES/5516.misc new file mode 100644 index 00000000000..966c85cc9b4 --- /dev/null +++ b/CHANGES/5516.misc @@ -0,0 +1 @@ +Removed @unittest_run_loop. This is now the default behaviour. diff --git a/aiohttp/test_utils.py b/aiohttp/test_utils.py index dd6c91c368e..e8ebb6d4ed0 100644 --- a/aiohttp/test_utils.py +++ b/aiohttp/test_utils.py @@ -2,7 +2,6 @@ import asyncio import contextlib -import functools import gc import inspect import ipaddress @@ -456,21 +455,6 @@ async def get_client(self, server: TestServer) -> TestClient: return TestClient(server) -def unittest_run_loop(func: Any, *args: Any, **kwargs: Any) -> Any: - """A decorator dedicated to use with asynchronous methods of an - AioHTTPTestCase. - - Handles executing an asynchronous function, using - the self.loop of the AioHTTPTestCase. - """ - - @functools.wraps(func, *args, **kwargs) - def new_func(self: Any, *inner_args: Any, **inner_kwargs: Any) -> Any: - return self.loop.run_until_complete(func(self, *inner_args, **inner_kwargs)) - - return new_func - - _LOOP_FACTORY = Callable[[], asyncio.AbstractEventLoop] diff --git a/docs/testing.rst b/docs/testing.rst index f5895617523..a93876e9497 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -290,7 +290,7 @@ Unittest To test applications with the standard library's unittest or unittest-based functionality, the AioHTTPTestCase is provided:: - from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop + from aiohttp.test_utils import AioHTTPTestCase from aiohttp import web class MyAppTestCase(AioHTTPTestCase): @@ -306,26 +306,11 @@ functionality, the AioHTTPTestCase is provided:: app.router.add_get('/', hello) return app - # the unittest_run_loop decorator can be used in tandem with - # the AioHTTPTestCase to simplify running - # tests that are asynchronous - @unittest_run_loop async def test_example(self): - resp = await self.client.request("GET", "/") - assert resp.status == 200 - text = await resp.text() - assert "Hello, world" in text - - # a vanilla example - def test_example_vanilla(self): - async def test_get_route(): - url = "/" - resp = await self.client.request("GET", url) - assert resp.status == 200 + async with self.client.request("GET", "/") as resp: + self.assertEqual(resp.status, 200) text = await resp.text() - assert "Hello, world" in text - - self.loop.run_until_complete(test_get_route()) + self.assertIn("Hello, world", text) .. class:: AioHTTPTestCase @@ -408,25 +393,13 @@ functionality, the AioHTTPTestCase is provided:: .. note:: The ``TestClient``'s methods are asynchronous: you have to - execute function on the test client using asynchronous methods. - - A basic test class wraps every test method by - :func:`unittest_run_loop` decorator:: + execute functions on the test client using asynchronous methods.:: class TestA(AioHTTPTestCase): - @unittest_run_loop async def test_f(self): - resp = await self.client.get('/') - - -.. decorator:: unittest_run_loop: - - A decorator dedicated to use with asynchronous methods of an - :class:`AioHTTPTestCase`. - - Handles executing an asynchronous function, using - the :attr:`AioHTTPTestCase.loop` of the :class:`AioHTTPTestCase`. + async with self.client.get('/') as resp: + body = await resp.text() Patching unittest test cases ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/test_loop.py b/tests/test_loop.py index 3211b756702..50fe5a8ad69 100644 --- a/tests/test_loop.py +++ b/tests/test_loop.py @@ -6,7 +6,7 @@ import pytest from aiohttp import web -from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop +from aiohttp.test_utils import AioHTTPTestCase @pytest.mark.skipif( @@ -34,7 +34,6 @@ async def get_application(self) -> web.Application: async def on_startup_hook(self, app: Any) -> None: self.on_startup_called = True - @unittest_run_loop async def test_on_startup_hook(self) -> None: self.assertTrue(self.on_startup_called) diff --git a/tests/test_test_utils.py b/tests/test_test_utils.py index 9ef27154846..2a540f104e3 100644 --- a/tests/test_test_utils.py +++ b/tests/test_test_utils.py @@ -16,7 +16,6 @@ TestServer as _TestServer, loop_context, make_mocked_request, - unittest_run_loop, ) _hello_world_str = "Hello, world" @@ -90,7 +89,6 @@ class TestAioHTTPTestCase(AioHTTPTestCase): def get_app(self): return _create_example_app() - @unittest_run_loop async def test_example_with_loop(self) -> None: request = await self.client.request("GET", "/") assert request.status == 200