python - Pytest 引发不适用于自定义异常
问题描述
我在 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
任何帮助将不胜感激,因为我在这里完全被难住了。
解决方案
推荐阅读
- android - Android Studio (AS) 和 Emulator 工作正常,但 AS 无法识别模拟器
- android - 为什么你必须在 RxJava 中的 CompositeDisposable 上处理一个东西?
- sql - 将行转换为更多列
- postgresql - PITR 使用 docker 主从
- tensorflow - TensorFlow 权重的形状
- php - 配置:错误:请重新安装 BZip2 发行版
- wordpress - 阻止管理员在 Woocommerce 中为缺货产品下订单
- sql - 由于“ACTIVE_TRANSACTION”,数据库 [database-name] 的事务日志已满
- wordpress - 为什么 wordpress Houzez 的主题搜索功能不起作用?
- node.js - 本地主机没有发送任何数据。当我在端口 3000 上运行服务器时出现 ERR_EMPTY_RESPONSE 结果,即使我设置了谷歌凭据