【发布时间】:2020-02-20 21:24:38
【问题描述】:
为什么下面的测试永远挂起?
import asyncio
import unittest
class TestCancellation(unittest.IsolatedAsyncioTestCase):
async def test_works(self):
task = asyncio.create_task(asyncio.sleep(5))
await asyncio.sleep(2)
task.cancel()
await task
if __name__ == '__main__':
unittest.main()
【问题讨论】:
-
如果您在等待取消的任务时发现
CancelledError异常,事情就会顺利进行。所以我猜测试运行器没有正确完成它的工作 -
您可能会获得更多见解here
-
我发现 pytest-asyncio 在异步测试方面比 unitest lib 更可靠。
标签: python python-3.x unit-testing python-asyncio python-3.8