出于测试目的,尝试模拟aiohttp.ClientSession的响应
我的代码如下所示:
async def run_request(endpoint: str, **kwargs) -> dict:
async with aiohttp.ClientSession() as session:
async with session.post(endpoint, **kwargs) as response:
response_json = await response.json()
return {"response": response, "response_json": response_json}我想用我想象中的东西来测试我的代码:
@patch("aiohttp.ClientSession.post", <something>)
def test_run_request(self):我该怎么做呢?
发布于 2020-02-11 20:42:59
对于这个问题有很多不同的解决方案,我认为最简单的方法是使用aioresponse库。(https://github.com/pnuckowski/aioresponses)
但是,只有当您只使用ClientSession而不使用aiohttp.web.Application时,这才能正常工作。如果你使用CS作为aiohttp.web.Application的一部分,你会破坏应用程序test_client (pytest-aiohttp)。
另请参阅此问题How to mock aiohttp.client.ClientSession.get async context manager。这里有一些有用的例子。
https://stackoverflow.com/questions/60127643
复制相似问题