Unit testing tornado在使用AsyncHttpTastCase测试异步方法时被阻止

Unit testing tornado在使用AsyncHttpTastCase测试异步方法时被阻止,unit-testing,asynchronous,tornado,Unit Testing,Asynchronous,Tornado,我不熟悉python和tornado,正在尝试使用@tornado.web.asynchronous为GET方法编写单元测试。但它始终被阻止并输出以下错误消息: Failure Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 327, in run testMetho

我不熟悉python和tornado,正在尝试使用@tornado.web.asynchronous为GET方法编写单元测试。但它始终被阻止并输出以下错误消息:

Failure
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 327, in run
    testMethod()
File "/Developer/PycharmProjects/CanMusic/tornadotestcase.py", line 19, in test_gen
    response = self.fetch(r'/gen')
File "/Developer/python/canmusic_env/lib/python2.7/site-packages/tornado/testing.py", line 265, in fetch
    return self.wait()
File "/Developer/python/canmusic_env/lib/python2.7/site-packages/tornado/testing.py", line 205, in wait
    self.__rethrow()
File "/Developer/python/canmusic_env/lib/python2.7/site-packages/tornado/testing.py", line 149, in __rethrow
    raise_exc_info(failure)
File "/Developer/python/canmusic_env/lib/python2.7/site-packages/tornado/testing.py", line 186, in timeout_func
    timeout)
AssertionError: Async operation timed out after 5 seconds
我编写以下代码作为示例。将其作为单元测试(nose)运行,获取上面的错误。当它作为一个独立的应用程序运行并通过浏览器访问url时,它工作正常。我还尝试了异步回调版本(没有@tornado.gen.engine和yield),结果也是一样的

为什么??我错过什么了吗

import tornado.web, tornado.gen, tornado.httpclient, tornado.testing

class GenHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self):
        http = tornado.httpclient.AsyncHTTPClient()
        response = yield tornado.gen.Task(http.fetch, 'http://www.google.com')
        self.finish()

# code for run nose test
class GenTestCase(tornado.testing.AsyncHTTPTestCase):
    def get_app(self):
        return tornado.web.Application([(r'/gen', GenHandler)])
    def test_gen(self):
        response = self.fetch(r'/gen')
        assert response.code == 200

# code for run standalone
application = tornado.web.Application([(r'/gen', GenHandler),])
if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

发生的情况是,RequestHandler中的AsyncHTTPClient正在另一个IOLoop上运行,而IOLoop从未启动

您可以通过在测试用例中覆盖
get\u new\u ioloop
来解决此问题:

def get_new_ioloop(self):
    return tornado.ioloop.IOLoop.instance()

这有点奇怪,但之所以会出现这种情况,是因为每个AsyncTestCase/AsyncHTTPTestCase方法都会生成自己的IOLoop,以实现更好的隔离。

对于我的情况,默认超时是不够的,因为我正在对远程服务进行集成测试

我在安装方法中覆盖默认超时

def setUp(self):
    super(AsyncHTTPTestCase, self).setUp()
    ## allow more time before timeout since we are doing remote access..
    os.environ["ASYNC_TEST_TIMEOUT"] = str(60)

谢谢,但它不起作用:TypeError:fetch()只接受2个参数(给定3个)。self.fetch()已异步调用AsyncHTTPClient.fetch(),请参阅源代码。医生说他们“相当”。试过这个,但没有运气。试过这个,但没有运气