Repository: pytest-dev/pytest-asyncio

Issue #170: Release 0.14.0 yields RuntimeError: Timeout context manager should be used inside a task
Created: 2020-06-26T10:32:14Z
I am using pytest-asyncio together with pytest-aiohttp.
When trying to establish a client session in an async testcase I get:
RuntimeError: Timeout context manager should be used inside a task

for pytest-asyncio 0.14.0. The same works with 0.12.0
Example:
from aiohttp.web import Application
from pytest import mark


@mark.asyncio
async def test_client(aiohttp_client):
    app = Application()
    client = await aiohttp_client(app)
    await client.get('/')
On 0.12.0 this code succeeds, on 0.14.0 it produces the following error:
============================================================================================ FAILURES ============================================================================================= 
_______________________________________________________________________________________ test_client[pyloop] _______________________________________________________________________________________ 

aiohttp_client = <function aiohttp_client.<locals>.go at 0x000001DBCCA19AE8>

    @mark.asyncio
    async def test_client(aiohttp_client):
        app = Application()
        client = await aiohttp_client(app)
>       await client.get('/')

test\test_asyncio.py:9:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.venv\lib\site-packages\aiohttp\test_utils.py:292: in _request
    method, self.make_url(path), **kwargs
.venv\lib\site-packages\aiohttp\client.py:426: in _request
    with timer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

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

    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

.venv\lib\site-packages\aiohttp\helpers.py:579: RuntimeError
======================================================================================== warnings summary ========================================================================================= 
test/test_asyncio.py::test_client[pyloop]
  XXX\.venv\lib\site-packages\aiohttp\cookiejar.py:55: DeprecationWarning: The object should be created from async function
    super().__init__(loop=loop)

test/test_asyncio.py::test_client[pyloop]
  XXX\.venv\lib\site-packages\aiohttp\test_utils.py:251: DeprecationWarning: The object should be created from async function
    **kwargs)

test/test_asyncio.py::test_client[pyloop]
  XXX\.venv\lib\site-packages\aiohttp\connector.py:731: DeprecationWarning: The object should be created from async function
    loop=loop)

test/test_asyncio.py::test_client[pyloop]
  XXX\.venv\lib\site-packages\aiohttp\connector.py:736: DeprecationWarning: The object should be created from async function
    resolver = DefaultResolver(loop=self._loop)

-- Docs: https://docs.pytest.org/en/latest/warnings.html
===================================================================================== short test summary info ===================================================================================== 
FAILED test/test_asyncio.py::test_client[pyloop] - RuntimeError: Timeout context manager should be used inside a task
========================================================================== 1 failed, 10 deselected, 4 warnings in 0.83s =========================================================================== 

Version of pytest-aiohttp is 0.3.0

Duplicate issue #185: RuntimeError when testing an aiohttp application
Created: 2020-09-02T14:22:20Z
After upgrading pytest-asyncio from 0.12.0 to 0.14.0, I started to get a RuntimeError when testing an aiohttp application. A reduced testcase looks like this:
import pytest
from aiohttp import web


@pytest.mark.asyncio
async def test_ping(aiohttp_client):
    async def handle_ping(request):
        return web.Response(text='pong')

    app = web.Application()
    app.router.add_get('/ping', handle_ping)

    client = await aiohttp_client(app)
    response = await client.get('/ping')

    assert response.status == 200
    assert await response.text() == 'pong'
When run using pytest, I get this failure, which wasn’t present before:
______________________________ test_ping[pyloop] _______________________________

aiohttp_client = <function aiohttp_client.<locals>.go at 0x10ddf6af0>

    @pytest.mark.asyncio
    async def test_ping(aiohttp_client):
        async def handle_ping(request):
            return web.Response(text='pong')
    
        app = web.Application()
        app.router.add_get('/ping', handle_ping)
    
        client = await aiohttp_client(app)
>       response = await client.get('/ping')

test_app.py:14: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.venv/lib/python3.8/site-packages/aiohttp/test_utils.py:291: in _request
    resp = await self._session.request(
.venv/lib/python3.8/site-packages/aiohttp/client.py:426: in _request
    with timer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

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

    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

.venv/lib/python3.8/site-packages/aiohttp/helpers.py:579: RuntimeError

Here are the package versions used:
aiohttp==3.6.2
pytest==6.0.1
pytest-aiohttp==0.3.0
pytest-asyncio==0.14.0

For full pytest output both with and without the bug, see this Gist.

Closing comment (2021-01-04T08:38:05Z): This workaround might help you: #170 (comment)

Thanks! I guess I’ll just stay with 0.12.0 for the time being though (every time I write loop in my asyncio code, a kitten dies).
In any case, this issue looks like a duplicate of #170 which I somehow missed. Closing.