如何使用python 3.8 unittest IsolatedAsyncioTestCase处理CancelledError

我想问有关处理 IsolatedAsyncioTestCase 中的asyncio.CancelledError的问题,该信息用于在py3.8上测试asyncio

给出以下测试用例

import unittest
from unittest import IsolatedAsyncioTestCase
import asyncio


class Test(IsolatedAsyncioTestCase):
    def setUp(self):
        print('setup')

    async def asyncSetUp(self):
        print('async setup')

    async def test_response(self):
        print('test_response')
        self.addAsyncCleanup(self.on_cleanup)

    def tearDown(self):
        print('teardown')

    async def asyncTearDown(self):
        print('before async teardown')
        fut = asyncio.Future()
        fut.cancel()
        await fut
        print('after async teardown')

    async def on_cleanup(self):
        print('clean up')


if __name__ == "__main__":
    unittest.main()

在拆解时很烂

root@1318a3fe59d0:/# python --version
Python 3.8.0
root@1318a3fe59d0:/# python /workspace/b.py 
setup
async setup
test_response
before async teardown
....

在深入到错误堆栈(通过按CTRL + C终止过程)和github中的源代码之后,这些都是与此错误相关的代码。

错误 asyncio.CancelledError 已有选择地提出,但没有其他例外。因此,我想知道如何捕获并处理此错误,而不是挂在终端上。

lh2009168 回答:如何使用python 3.8 unittest IsolatedAsyncioTestCase处理CancelledError

最近,我从 python 的票务系统中找到了一个 checking what they modified

希望它在 2020-12-16 得到解决。此外,通过 3.8 分支中的 enter image description here,它们改变了捕获异常的方式。

因此,只要您拥有包含此修复程序的最新 Python,您就不会再遇到此问题。

本文链接:https://www.f2er.com/3146724.html

大家都在问