Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove @unittest_run_loop #5516

Merged
merged 8 commits into from Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/5516.misc
@@ -0,0 +1 @@
Removed @unittest_run_loop. This is now the default behaviour.
16 changes: 0 additions & 16 deletions aiohttp/test_utils.py
Expand Up @@ -2,7 +2,6 @@

import asyncio
import contextlib
import functools
import gc
import inspect
import ipaddress
Expand Down Expand Up @@ -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]


Expand Down
41 changes: 7 additions & 34 deletions docs/testing.rst
Expand Up @@ -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):
Expand All @@ -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

Expand Down Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
3 changes: 1 addition & 2 deletions tests/test_loop.py
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 0 additions & 2 deletions tests/test_test_utils.py
Expand Up @@ -16,7 +16,6 @@
TestServer as _TestServer,
loop_context,
make_mocked_request,
unittest_run_loop,
)

_hello_world_str = "Hello, world"
Expand Down Expand Up @@ -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
Expand Down