Python 最可怕的龙卷风';SimpleAsyncHTTPClient';这是不可容忍的
试图在PyTest、Tornado下为长轮询编写测试代码 我的测试代码如下 conftest.pyPython 最可怕的龙卷风';SimpleAsyncHTTPClient';这是不可容忍的,python,tornado,pytest,Python,Tornado,Pytest,试图在PyTest、Tornado下为长轮询编写测试代码 我的测试代码如下 conftest.py from tornado.httpclient import AsyncHTTPClient @pytest.fixture async def tornado_server(): print("\ntornado_server()") @pytest.fixture async def http_client(tornado_server): client = Async
from tornado.httpclient import AsyncHTTPClient
@pytest.fixture
async def tornado_server():
print("\ntornado_server()")
@pytest.fixture
async def http_client(tornado_server):
client = AsyncHTTPClient()
return client
@pytest.yield_fixture(scope='session')
def event_loop(request):
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
测试我的
from tornado.httpclient import HTTPRequest, HTTPError
def test_http_client(event_loop):
url = 'http://httpbin.org/get'
resp = event_loop.run_until_complete(http_client(url))
assert b'HTTP/1.1 200 OK' in resp
我期望这个结果会成功。
但它失败了
我做错了什么?在test\u http\u client()函数中,尝试在resp.code中断言“200”,或在resp.reason中断言“OK”
分配给resp
的对象是AsyncHTTPClient,而不是响应本身。要调用响应消息,您需要类似于resp.code、resp.reason、resp.body、resp.headers等内容
这是一张你可以打电话的清单
要使用pytest fixture,必须将其列为函数的参数:
def test_http_client(event_loop, http_client):
AsyncHTTPClient不可调用;它有一个fetch
方法:
resp = event_loop.run_until_complete(http_client.fetch(url))
代码中发生的事情是调用fixture,而不是让pytest对其进行初始化,并将url
作为其tornado\u服务器
参数传递
也可以考虑使用<代码> PyTestAssiCIO < /C>或<代码> PyTestTurnADO/COD>,它允许您使用<代码>等待<代码>而不是<代码> Run-UnLILUTION>代码>(这是使用PyTestToRoTo或AsiCIIO的常用方法):
resp = event_loop.run_until_complete(http_client.fetch(url))
@pytest.mark.asyncio
async def test_http_client(http_client):
url = 'http://httpbin.org/get'
resp = await http_client.fetch(url)
assert resp.code == 200