英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论