Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 最可怕的龙卷风';SimpleAsyncHTTPClient';这是不可容忍的_Python_Tornado_Pytest - Fatal编程技术网

Python 最可怕的龙卷风';SimpleAsyncHTTPClient';这是不可容忍的

Python 最可怕的龙卷风';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

试图在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 = 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