PyTest Tornado: 'SimpleAsyncHTTPClient' is not iterable

PyTest Tornado: 'SimpleAsyncHTTPClient' is not iterable



Trying to make test code for long polling under PyTest, Tornado.



My test code is at below.



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()



test_my.py


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



I expected this result finish as success.
But it failed.


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 E TypeError: argument of type 'SimpleAsyncHTTPClient' is not iterable



What did I do wrong?




2 Answers
2



To use a pytest fixture, you must list it as an argument to your function:


def test_http_client(event_loop, http_client):



AsyncHTTPClient is not callable; it has a fetch method:


fetch


resp = event_loop.run_until_complete(http_client.fetch(url))



What's happening in your code is that you're calling the fixture instead of letting pytest initialize it, and passing url as its tornado_server argument.


url


tornado_server



Also consider using pytest-asyncio or pytest-tornado, which lets you use await instead of run_until_complete (this is the usual method of using pytest with tornado or asyncio):


pytest-asyncio


pytest-tornado


await


run_until_complete


@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



Try assert "200" in resp.code or assert "OK" in resp.reasonin your test_http_client() function.


assert "200" in resp.code


assert "OK" in resp.reason



The object that is getting assigned to resp is the AsyncHTTPClient, not the response itself. To call the response message, you need something like resp.code, resp.reason, resp.body, resp.headers, etc.


resp


resp.code, resp.reason, resp.body, resp.headers



Here's a list of things that you can call http://www.tornadoweb.org/en/stable/httpclient.html#response-objects



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PHP code is not being executed, instead code shows on the page

Administrative divisions of China

Cardinality of the set of algorithms