When I run pytest, errors occurred.

I’m thinking to want to test about aiohttp application.
First, I tried to do this(https://docs.aiohttp.org/en/stable/testing.html?highlight=pytest#pytest_aiohttp.aiohttp_client).

from aiohttp import web

async def hello(request):
    return web.Response(text='Hello, world')

async def test_hello(aiohttp_client, loop):
    app = web.Application()
    app.router.add_get('/', hello)
    client = await aiohttp_client(app)
    resp = await client.get('/')
    assert resp.status == 200
    text = await resp.text()
    assert 'Hello, world' in text

When I run pytest, errors occurred.
I don’t know why errors occur.
Could you tell me some advice?

This is my environment.

macOS Catalina Version 10.15.5
Python 3.8.2

aiohttp          3.6.2
pytest           5.4.3
pytest-aiohttp   0.3.0
pytest-asyncio   0.14.0

The error is as below.

============================= test session starts ==============================
platform darwin -- Python 3.8.2, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /Users/konishiyuji/workspaces/pytest/asyncio
plugins: mock-3.2.0, aiohttp-0.3.0, asyncio-0.14.0
collected 2 items

test_aiohttp.py F                                                        [ 50%]
test_say.py .                                                            [100%]

=================================== FAILURES ===================================
______________________________ test_hello[pyloop] ______________________________

aiohttp_client = <function aiohttp_client.<locals>.go at 0x108456b80>
loop = <_UnixSelectorEventLoop running=False closed=False debug=False>

    async def test_hello(aiohttp_client, loop):
        app = web.Application()
        app.router.add_get('/', hello)
        client = await aiohttp_client(app)
>       resp = await client.get('/')

test_aiohttp.py:10: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../../.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/test_utils.py:291: in _request
    resp = await self._session.request(
../../../.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/client.py:426: in _request
    with timer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <aiohttp.helpers.TimerContext object at 0x1084749a0>

    def __enter__(self) -> BaseTimerContext:
        task = current_task(loop=self._loop)
    
        if task is None:
>           raise RuntimeError('Timeout context manager should be used '
                               'inside a task')
E           RuntimeError: Timeout context manager should be used inside a task

../../../.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/helpers.py:579: RuntimeError
=============================== warnings summary ===============================
test_aiohttp.py::test_hello[pyloop]
  /Users/konishiyuji/.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/cookiejar.py:55: DeprecationWarning: The object should be created from async function
    super().__init__(loop=loop)

test_aiohttp.py::test_hello[pyloop]
  /Users/konishiyuji/.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/test_utils.py:249: DeprecationWarning: The object should be created from async function
    self._session = ClientSession(loop=loop,

test_aiohttp.py::test_hello[pyloop]
  /Users/konishiyuji/.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/connector.py:727: DeprecationWarning: The object should be created from async function
    super().__init__(keepalive_timeout=keepalive_timeout,

test_aiohttp.py::test_hello[pyloop]
  /Users/konishiyuji/.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/connector.py:736: DeprecationWarning: The object should be created from async function
    resolver = DefaultResolver(loop=self._loop)

test_aiohttp.py::test_hello[pyloop]
  /Users/konishiyuji/.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/web_server.py:53: DeprecationWarning: The loop argument is deprecated since Python 3.8, and scheduled for removal in Python 3.10.
    await asyncio.gather(*coros, loop=self._loop)

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========================== short test summary info ============================
FAILED test_aiohttp.py::test_hello[pyloop] - RuntimeError: Timeout context ma...
=================== 1 failed, 1 passed, 5 warnings in 0.27s ====================

I could remove this error by using “@pytest.fixture” as below.
Is this a correct way?

@pytest.fixture
def loop():
    loop = asyncio.get_event_loop()
    yield loop
    loop.close()

async def hello(request):
    return web.Response(text='Hello, world')

async def test_hello(aiohttp_client, loop):
    app = web.Application()
    app.router.add_get('/', hello)
    client = await aiohttp_client(app)
    resp = await client.get('/')
    assert resp.status == 200
    text = await resp.text()
    assert 'Hello, world' in text

Use the pytest-asyncio plugin. Just install it along with pytest, and add @pytest.mark.asyncio to your async test cases. It will handle event loop mgmt and wrap your tests as tasks.

I installed “pytest-asyncio” by using “pip”.
And I ran this program that was added “@pytest.mark.asyncio”.
But errors occurred again.

from aiohttp import web                                                         
import pytest
import asyncio

async def hello(request):
    return web.Response(text='Hello, world')

@pytest.mark.asyncio
async def test_hello(aiohttp_client, loop):
    app = web.Application()
    app.router.add_get('/', hello)
    client = await aiohttp_client(app)
    resp = await client.get('/')
    assert resp.status == 200
    text = await resp.text()
    assert 'Hello, world' in text

The error is as below.

=================================== FAILURES ===================================
______________________________ test_hello[pyloop] ______________________________

aiohttp_client = <function aiohttp_client.<locals>.go at 0x10f7135e0>
loop = <_UnixSelectorEventLoop running=False closed=False debug=False>

    @pytest.mark.asyncio
    async def test_hello(aiohttp_client, loop):
        app = web.Application()
        app.router.add_get('/', hello)
        client = await aiohttp_client(app)
>       resp = await client.get('/')

test_aiohttp.py:13: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../../.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/test_utils.py:291: in _request
    resp = await self._session.request(
../../../.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/client.py:426: in _request
    with timer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <aiohttp.helpers.TimerContext object at 0x10f73b3d0>

    def __enter__(self) -> BaseTimerContext:
        task = current_task(loop=self._loop)
    
        if task is None:
>           raise RuntimeError('Timeout context manager should be used '
                               'inside a task')
E           RuntimeError: Timeout context manager should be used inside a task

../../../.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/helpers.py:579: RuntimeError
=============================== warnings summary ===============================
test_aiohttp.py::test_hello[pyloop]
  /Users/konishiyuji/.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/cookiejar.py:55: DeprecationWarning: The object should be created from async function
    super().__init__(loop=loop)

test_aiohttp.py::test_hello[pyloop]
  /Users/konishiyuji/.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/test_utils.py:249: DeprecationWarning: The object should be created from async function
    self._session = ClientSession(loop=loop,

test_aiohttp.py::test_hello[pyloop]
  /Users/konishiyuji/.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/connector.py:727: DeprecationWarning: The object should be created from async function
    super().__init__(keepalive_timeout=keepalive_timeout,

test_aiohttp.py::test_hello[pyloop]
  /Users/konishiyuji/.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/connector.py:736: DeprecationWarning: The object should be created from async function
    resolver = DefaultResolver(loop=self._loop)

test_aiohttp.py::test_hello[pyloop]
  /Users/konishiyuji/.pyenv/versions/3.8.2/lib/python3.8/site-packages/aiohttp/web_server.py:53: DeprecationWarning: The loop argument is deprecated since Python 3.8, and scheduled for removal in Python 3.10.
    await asyncio.gather(*coros, loop=self._loop)

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========================== short test summary info ============================
FAILED test_aiohttp.py::test_hello[pyloop] - RuntimeError: Timeout context ma...
======================== 1 failed, 5 warnings in 0.26s =========================