简体   繁体   English

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

[英]Run tornado.testing.AsyncTestCase using asyncio event loop

I have an asyncio based class which I want to unit test. 我有一个基于asyncio的类,我想进行单元测试。 Using tornado.testing.AsyncTestCase this works quite well and easily. 使用tornado.testing.AsyncTestCase这非常有效。 However, one specific method of my class uses asyncio.ensure_future to schedule execution of another method. 但是,我的类的一个特定方法使用asyncio.ensure_future来安排另一个方法的执行。 This never finishes in the AsyncTestCase , because the default test runner uses the tornado KQueueIOLoop event loop, not an asyncio event loop. 这永远不会在AsyncTestCase完成,因为默认测试运行器使用龙卷风KQueueIOLoop事件循环,而不是asyncio事件循环。

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

    async def bar(self):
        pass
class TestSubjectTest(AsyncTestCase):
    def test_foo(self):
        t = TestSubject()
        # here be somewhat involved setup with MagicMock and self.stop
        t.foo()
        self.wait()
$ 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

How can I use a different event loop to run the tests on to ensure my task will actually be executed? 如何使用不同的事件循环来运行测试以确保我的任务实际执行? Alternatively, how can I make my implementation event loop-independent and cross-compatible? 或者,如何使我的实现事件独立于循环并交叉兼容?

Turns out to be simple enough... 结果很简单......

class TestSubjectTest(AsyncTestCase):
    def get_new_ioloop(self):  # override this method
        return tornado.platform.asyncio.AsyncIOMainLoop()

I was trying this before, but directly returned asyncio.get_event_loop() , which didn't work. 我之前尝试过这个,但是直接返回了asyncio.get_event_loop() ,但是没有用。 Returning Tornado's asyncio loop wrapper does the trick. 返回Tornado的asyncio循环包装就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 tornado.testing.AsyncTestCase 测试可以有一个抽象基类吗? - Can I have an abstract base class for my tornado.testing.AsyncTestCase tests? 如何将龙卷风 ioloop 设置为 asyncio event_loop? - How do I set the tornado ioloop to an asyncio event_loop? python asyncio运行事件循环一次? - python asyncio run event loop once? asyncio 事件循环是否只运行任务? - Does the asyncio event loop only run tasks? 使用 Tornado 发出同步请求。 RuntimeError:在另一个循环正在运行时无法运行事件循环 - Making synchronous requests using Tornado. RuntimeError: Cannot run the event loop while another loop is running 在主线程运行的asyncio事件循环中运行无限循环 - run infinite loop in asyncio event loop running off the main thread 在没有 asyncio.get_event_loop() 的 run_in_executor 的异步方法中使用线程池 - Using Threadpool in an Async method without run_in_executor of asyncio.get_event_loop() 在asyncio中使用run_in_executor时,事件循环是否在主线程中执行? - When using run_in_executor in asyncio, is the event loop executed in the main thread? 使用 Jupyter Notebook 时“无法从正在运行的事件循环中调用 asyncio.run()” - "asyncio.run() cannot be called from a running event loop" when using Jupyter Notebook 如何在Kivy GUI旁边运行Tornado事件循环? - How to run the Tornado event loop alongside a Kivy GUI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM