您可以使用 self.assertRaises 作为异步上下文管理器吗?

Can you use self.assertRaises as an async context manager?

我想测试 python 3 coro 因特定异常而失败,但似乎没有实现此功能。

async with self.assertRaises(TestExceptionType):
    await my_func()

因为单元测试失败是这样的:

...
File "/Users/...../tests.py", line 144, in go
    async with self.assertRaises(TestExceptionType):
AttributeError: __aexit__

所以我的问题是:这可行吗?如果不是,断言失败的异步函数的最佳方法是什么?

就用经典老好with左右await来电:

with self.assertRaises(TestExceptionType):
    await my_func()

有效。