我在exceptions.py文件中定义了以下内容:
class Error(Exception):
"""Base exception raised by api wrapper"""
def __init__(self, message: str):
self.message = message
super().__init__(self.message)
# HTTP response exceptions
class ApiBadRequestError(Error):
"""Bad Request –- Incorrect parameters."""
def __init__(self, message: str):
self.message = message
super().__init__(self.message)
然后,我有一个函数可以正确地引发ApiBadRequestError
异常
在pytest中,我将执行以下操作:
def test_handle_request_response_raises_correct_exception_for_response_code(
self, status_code, exception_type, client, create_response
):
response = create_response(status_code=status_code)
with pytest.raises(ApiBadRequestError) as e:
a = client._check_response_codes(response)
它没有通过测试,因为在pytest.raises
内它正在执行isintance(e, ApiBadRequestError)
,返回False。但是,如果我将测试更改为以下内容:
def test_handle_request_response_raises_correct_exception_for_response_code(
self, status_code, exception_type, client, create_response
):
response = create_response(status_code=status_code)
with pytest.raises(Exception) as e:
a = client._check_response_codes(response)
当引发的异常被视为Exception
的实例时,即使它是ApiBadRequestError
的实例,它也会传递
任何帮助都将不胜感激,因为我在这里完全被难住了
目前没有回答
相关问题 更多 >
编程相关推荐