我有一些在 python 3 中使用自定义异常的代码。
就像是:
def get_my_custom_error():
try:
1.0/0.0
except ZeroDivisionError as err:
raise MyCustomError() from err
Run Code Online (Sandbox Code Playgroud)
在我的测试文件中我有以下内容:
with pytest.raises(MyCustomError):
get_my_custom_error()
Run Code Online (Sandbox Code Playgroud)
我目前得到的输出如下
ZeroDivisionError
the above exception was the direct cause of the following error:
MyCustomError
Run Code Online (Sandbox Code Playgroud)
这会导致测试失败。
所以代码似乎可以工作,但 pytest 似乎没有检查最高级别的错误(这就是我希望它做的)。
Python 3.6.1 :: Anaconda 4.4.0 pytest 3.0.7
任何帮助都会很棒。