python - 有没有办法覆盖pytest(python)中的默认断言?

标签 python pytest

我想在每次调用断言时将一些信息记录到文件/数据库中。有没有办法在每次调用断言时覆盖断言或注册某种回调函数来执行此操作?

问候 沙拉德

最佳答案

尝试重载 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/

相关文章:

Gitlab CI变量,带引号的选项

python-3.x - 用线程运行 pytest

python - pytest 的 ModuleNotFoundError 问题

python - PyCharm 远程部署 : user name not being saved

python - 如何有条件地标记组?

Python 线程并行化逃离 GIL

python - 如何在 PyCharm 中运行特定测试

python - python中的 "for key in dict"是否总是以固定顺序迭代?

python - 有没有办法在 PyCharm 调试器中查看变量内的最后 100 个元素?

python - 如何在 gitlab CI/CD 管道中使用 pytest 测试 Flask 应用程序时忽略某些脚本?