我想测试 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__
所以我的问题是:这可行吗?如果不是,断言失败的异步函数的最佳方法是什么?
最佳答案
只需在 await
调用周围使用经典的旧好 with
即可:
with self.assertRaises(TestExceptionType):
await my_func()
有效。
关于python - 您可以将 self.assertRaises 用作异步上下文管理器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39492402/