簡體   English   中英

使用asyncio事件循環運行tornado.testing.AsyncTestCase

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

我有一個基於asyncio的類,我想進行單元測試。 使用tornado.testing.AsyncTestCase這非常有效。 但是,我的類的一個特定方法使用asyncio.ensure_future來安排另一個方法的執行。 這永遠不會在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

如何使用不同的事件循環來運行測試以確保我的任務實際執行? 或者,如何使我的實現事件獨立於循環並交叉兼容?

結果很簡單......

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

我之前嘗試過這個,但是直接返回了asyncio.get_event_loop() ,但是沒有用。 返回Tornado的asyncio循環包裝就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM