Search code examples
pythonmockingpytestaiohttp

AttributeError: __aenter__ Pytest AioHTTP


I write this test, but I am given this error:

cont = 14322, name = 'reddis'

    async def logs(cont, name):
    
        conn = aiohttp.UnixConnector(path="/var/run/docker.sock")
    
        async with aiohttp.ClientSession(connector=conn) as session:
>           async with session.get(f"http://xx/containers/{cont}/logs?follow=1&stdout=1") as resp:
E           AttributeError: __aenter__

tests/test_logs_func.py:10: AttributeError

What am I doing wrong?

Test code:

import pytest
import aiohttp
from typing import List

async def logs(cont, name):

    conn = aiohttp.UnixConnector(path="/var/run/docker.sock")

    async with aiohttp.ClientSession(connector=conn) as session:
        async with session.get(f"http://xx/containers/{cont}/logs?follow=1&stdout=1") as resp:
            async for line in resp.content:
                return (name, line)

class MockResponse:
    content: List[str] = [
                "one",
                "two", 
                "three"
                ]

@pytest.mark.asyncio
async def test_logs_normal(monkeypatch):
    name = "reddis"
    cont = 14322

    monkeypatch.setattr(aiohttp.ClientSession, "get", lambda *args, **kwargs: MockResponse())
    res = await logs(cont=cont, name=name)
    assert res == (name, MockResponse.content[0]) 

In the original, the logs function does not return, but outputs data to the console and I need to write tests for this function.


Solution

  • monkeypatch.setattr(aiohttp.ClientSession, "get", lambda *args, **kwargs: MockResponse())
    

    This line tells me that you are mocking what ClientSession.get returns. The original ClientSession.get from aiohttp returns an async context manager (which you can use with async with), but you have replaced it with MockResponse which is not an async context manager.