AttributeError: __aenter__ Pytest AioHTTP

huangapple go评论76阅读模式
英文:

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.

英文:

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.

答案1

得分: 1

这行代码告诉我你正在模拟 ClientSession.get 返回的内容。原始的 aiohttp 中的 ClientSession.get 返回一个异步上下文管理器(可以使用 async with),但你已经将它替换为了 MockResponse,这不是一个异步上下文管理器。

英文:

> 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.

huangapple
  • 本文由 发表于 2023年7月3日 02:20:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定