Python 使用asyncio事件循环运行tornado.testing.AsyncTestCase

Python 使用asyncio事件循环运行tornado.testing.AsyncTestCase,python,unit-testing,python-3.x,tornado,python-asyncio,Python,Unit Testing,Python 3.x,Tornado,Python Asyncio,我有一个基于asyncio的类,我想对它进行单元测试。使用tornado.testing.AsyncTestCase这可以非常方便地工作。但是,我的类的一个特定方法使用了asyncio。请确保\u future计划另一个方法的执行。这在AsyncTestCase中永远不会结束,因为默认的测试运行程序使用tornadoKQueueIOLoop事件循环,而不是asyncio事件循环 class TestSubject: def foo(self): asyncio.ensur

我有一个基于asyncio的类,我想对它进行单元测试。使用
tornado.testing.AsyncTestCase
这可以非常方便地工作。但是,我的类的一个特定方法使用了
asyncio。请确保\u future
计划另一个方法的执行。这在
AsyncTestCase
中永远不会结束,因为默认的测试运行程序使用tornado
KQueueIOLoop
事件循环,而不是asyncio事件循环

class TestSubject:
    def foo(self):
        asyncio.ensure_future(self.bar())

    async def bar(self):
        pass
$python-m tornado.testing baz.testsubject\u test
...
[E 160627 17:48:22测试:731]失败
[E 160627 17:48:22基本事件:1090]任务已销毁,但挂起!
任务:
…/asyncio/base_events.py:362:RuntimeWarning:coroutine'TestSubject.bar'从未被等待过

如何使用不同的事件循环来运行测试,以确保实际执行任务?或者,如何使我的实现事件循环独立和交叉兼容?

证明非常简单

class TestSubjectTest(AsyncTestCase):
    def get_new_ioloop(self):  # override this method
        return tornado.platform.asyncio.AsyncIOMainLoop()
我以前尝试过这个,但直接返回了
asyncio.get\u event\u loop()
,但没有成功。返回Tornado的asyncio循环包装器就可以实现这一点

$ python -m tornado.testing baz.testsubject_test
...
[E 160627 17:48:22 testing:731] FAIL
[E 160627 17:48:22 base_events:1090] Task was destroyed but it is pending!
    task: <Task pending coro=<TestSubject.bar() running at ...>>
.../asyncio/base_events.py:362: RuntimeWarning: coroutine 'TestSubject.bar' was never awaited
class TestSubjectTest(AsyncTestCase):
    def get_new_ioloop(self):  # override this method
        return tornado.platform.asyncio.AsyncIOMainLoop()