我想在每次调用断言时将一些信息记录到文件/数据库中。有没有办法在每次调用断言时覆盖断言或注册某种回调函数来执行此操作?
问候 沙拉德
最佳答案
尝试重载 AssertionError
而不是 assert
。原始断言错误在 exceptions module 中可用在 python2 和 builtins 中python3中的模块。
import exceptions
class AssertionError:
def __init__(self, *args, **kwargs):
print("Log me!")
raise exceptions.AssertionError
关于python - 有没有办法覆盖pytest(python)中的默认断言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36160713/