aiohttp unittest with URLs not GET



我稍微摆弄了一下aiohttp文档中关于单元测试的示例,以了解示例中发生了什么以及它是如何工作的。我想我仍然误解一些事情。

我需要"模拟"从url下载xml或html文件。但是示例代码是关于GET方法的,因为它执行router.add_get()。但没有router.add_url()或类似的东西。

那么我的理解错了吗?

错误是

$ python3 -m unittest org.py
E
======================================================================
ERROR: test_example (org.MyAppTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/aiohttp/test_utils.py", line 439, in setUp
self.app = self.loop.run_until_complete(self.get_application())
File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/home/user/share/work/aiotest/org.py", line 16, in get_application
app.router.add_get('https://stackoverflow.com', hello)
File "/usr/local/lib/python3.9/dist-packages/aiohttp/web_urldispatcher.py", line 1158, in add_get
resource = self.add_resource(path, name=name)
File "/usr/local/lib/python3.9/dist-packages/aiohttp/web_urldispatcher.py", line 1071, in add_resource
raise ValueError("path should be started with / or be empty")
ValueError: path should be started with / or be empty
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
示例代码
#!/usr/bin/env python3
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
from aiohttp import web, ClientSession

class MyAppTestCase(AioHTTPTestCase):
async def get_application(self):
"""
Override the get_app method to return your application.
"""
async def hello(request):
return web.Response(text='Hello')
app = web.Application()
app.router.add_get('https://stackoverflow.com', hello)
return app
# the unittest_run_loop decorator can be used in tandem with
# the AioHTTPTestCase to simplify running
# tests that are asynchronous
@unittest_run_loop
async def test_example(self):
async with ClientSession() as session:
async with session.get('https://stackoverflow.com') as resp:
assert resp.status == 200
text = await resp.text()
assert 'Hello' in text

编辑:我的Python是3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110]和aiohttp是3.7.4。post0' -都来自Debian 11(稳定版)存储库。

不能用aiohttp.web.Application()路由器模拟特定地址。它希望你声明相对于应用程序根的路由,就像你提到的例子:

class MyAppTestCase(AioHTTPTestCase):
async def get_application(self):
"""
Override the get_app method to return your application.
"""
async def hello(request):
return web.Response(text='Hello, world')
app = web.Application()
app.router.add_get('/', hello)
return app

您应该使用self.client引用测试您在get_application方法中创建的应用程序,并使用相对路径:

@unittest_run_loop
async def test_example(self):
resp = await self.client.request("GET", "/")
assert resp.status == 200
text = await resp.text()
assert "Hello, world" in text

或者(你可能真正想要的)使用响应库:

@responses.activate
def test_simple():
responses.add(responses.GET, 'https://stackoverflow.com',
body='Hello', status=200)

相关内容

最新更新