AioHTTPTestCase Example with Python 3.6

Hi, we ran into this AssertionError bug in one of our services which prompted me to work through upgrading our version of aiohttp to the latest 3.8.1. This aiohttp update though makes a change to how unittests are handled. Our existing unit tests are now failing with TypeError: An asyncio.Future, a coroutine or an awaitable is required. We are using Python 3.6 which appears to have an extra dependency with asynctest although we already have that installed and are using it (do I need to explicitly inject this dependency anywhere? The PR doesn’t seem to have an example < 3.7 in it). I have also removed the @unittest_run_loop decorator since this appears to have been deprecated.

Our test case class inherits from AioHTTPTestCase, and the call that throws the error is (with real values in the ...):

async with as response:

The full error I’m seeing is:

/root/.pex/installed_wheels/e0e4d2b660dcc3b4f29b471afc56d17654b5049f/aiohttp-3.8.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl/aiohttp/ in _request
    resp = await self._session.request(method, self.make_url(path), **kwargs)
/root/.pex/installed_wheels/e0e4d2b660dcc3b4f29b471afc56d17654b5049f/aiohttp-3.8.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl/aiohttp/ in _request
    req, traces=traces, timeout=real_timeout
/root/.pex/installed_wheels/e0e4d2b660dcc3b4f29b471afc56d17654b5049f/aiohttp-3.8.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl/aiohttp/ in connect
    proto = await self._create_connection(req, traces, timeout)
/root/.pex/installed_wheels/e0e4d2b660dcc3b4f29b471afc56d17654b5049f/aiohttp-3.8.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl/aiohttp/ in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
/root/.pex/installed_wheels/e0e4d2b660dcc3b4f29b471afc56d17654b5049f/aiohttp-3.8.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl/aiohttp/ in _create_direct_connection
    hosts = await asyncio.shield(host_resolved)
/usr/local/lib/python3.6/asyncio/ in shield
    inner = ensure_future(arg, loop=loop)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

coro_or_future = <MagicMock name='mock()' id='140440515487616'>

    def ensure_future(coro_or_future, *, loop=None):
        """Wrap a coroutine or an awaitable in a future.
        If the argument is a Future, it is returned directly.
        if futures.isfuture(coro_or_future):
            if loop is not None and loop is not coro_or_future._loop:
                raise ValueError('loop argument must agree with Future')
            return coro_or_future
        elif coroutines.iscoroutine(coro_or_future):
            if loop is None:
                loop = events.get_event_loop()
            task = loop.create_task(coro_or_future)
            if task._source_traceback:
                del task._source_traceback[-1]
            return task
        elif compat.PY35 and inspect.isawaitable(coro_or_future):
            return ensure_future(_wrap_awaitable(coro_or_future), loop=loop)
>           raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
E           TypeError: An asyncio.Future, a coroutine or an awaitable is required

I have not been able to find an example of AioHTTPTestCase with python < 3.7 so I was hoping just a simple example would be enough to get me in the right direction. Thank you!

As an additional detail on this, it appears that if I run each test on its own, the test passes the same way it did before I upgraded the library. So there is potentially some interference between the tests?

Ended up being one of the unit tests that strangely caused all of them to fail. There was interaction between the tests, but once I fixed the one test, the rest succeeded. Strange.