简体   繁体   English

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

[英]Is there a way to override default assert in pytest (python)?

I'd like to a log some information to a file/database every time assert is invoked. 每次调用assert时,我都希望将一些信息记录到文件/数据库中。 Is there a way to override assert or register some sort of callback function to do this, every time assert is invoked? 每次调用assert时,有没有办法覆盖断言或注册某种回调函数来执行此操作?

Regards Sharad 关心沙拉德

I don't think that would be possible. 我认为那是不可能的。 assert is a statement (and not a function) in Python and has a predefined behavior. assert是Python中的一个语句(而不是函数),具有预定义的行为。 It's a language element and cannot just be modified. 它是一种语言元素,不能只是被修改。 Changing the language cannot be the solution to a problem. 更改语言不能解决问题。 Problem has to be solved using what is provided by the language 必须使用语言提供的内容来解决问题

There is one thing you can do though. 但是你可以做一件事。 Assert will raise AssertionError exception on failure. Assert会在失败时引发AssertionError异常。 This can be exploited to get the job done. 这可以被利用来完成工作。 Place the assert statement in Try-expect block and do your callbacks inside that block. assert statement放在Try-expect块中,并在该块内进行回调。 It isn't as good a solution as you are looking for. 它并不像您正在寻找的那样好。 You have to do this with every assert. 每个断言都必须这样做。 Modifying a statement's behavior is something one won't do. 修改语句的行为是人们不会做的事情。

Try overload the AssertionError instead of assert . 尝试重载AssertionError而不是assert The original assertion error is available in exceptions module in python2 and builtins module in python3. 原来的断言错误是可以例外模块在python2和建宏在python3模块。

import exceptions

class AssertionError:
    def __init__(self, *args, **kwargs):
        print("Log me!")
        raise exceptions.AssertionError

It is possible, because pytest is actually re-writing assert expressions in some cases. 这是可能的,因为pytest实际上在某些情况下重写了断言表达式。 I do not know how to do it or how easy it is, but here is the documentation explaining when assert re-writing occurs in pytest: https://docs.pytest.org/en/latest/assert.html 我不知道该怎么做或它有多容易,但这里是文档解释何时在pytest中发生断言重写: https ://docs.pytest.org/en/latest/assert.html

By default, if the Python version is greater than or equal to 2.6, py.test rewrites assert statements in test modules. 默认情况下,如果Python版本大于或等于2.6,py.test将重写测试模块中的断言语句。

... ...

py.test rewrites test modules on import. py.test在导入时重写测试模块。 It does this by using an import hook to write a new pyc files. 它通过使用导入钩子来编写新的pyc文件来实现此目的。

Theoretically, you could look at the pytest code to see how they do it, and perhaps do something similar. 理论上,您可以查看pytest代码以了解它们是如何做到的,也许可以做类似的事情。

For further information, Benjamin Peterson wrote up Behind the scenes of py.test's new assertion rewriting [ at http://pybites.blogspot.com/2011/07/behind-scenes-of-pytests-new-assertion.html ] 有关详细信息,本杰明彼得森写了py.test的新断言改写的幕后故事[ http://pybites.blogspot.com/2011/07/behind-scenes-of-pytests-new-assertion.html ]

I suggest to use pyhamcrest . 我建议使用pyhamcrest It has very beatiful matchers which can be simply reimplemented. 它有非常美丽的匹配器,可以简单地重新实现。 Also you can write your own. 你也可以自己写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以稳定的方式断言 pytest 列表? - How to assert lists with pytest in a stable way? 有没有办法在pytest(python)中使用assert注册回调方法/代码片段? - Is there a way to register a call-back method/code-snippet with assert in pytest (python)? 如何使用 python pytest 断言 2 个数据帧 - How to assert 2 data frames using python pytest 有什么方法可以检查 Python 子进程是否在设定的默认时间运行,不超过限制? 任何 Pytest/Mock/assert 想法? - What are the ways to check that Python sub processes are running in a set default time, not exceeding the limit? Any Pytest/Mock/assert ideas? 是否有一种干净的 Pythonic 方法来覆盖 Python 子类中的默认方法参数? - Is there a clean Pythonic way to override default method args in a subclass in Python? 是否可以在 Python 中更改 PyTest 的断言语句行为 - Is it possible to change PyTest's assert statement behaviour in Python Python的`assert False`如何阻止pytest工作? - How can Python's `assert False` prevent pytest from working? 如何使用pytest在python中断言二进制多行值 - how to assert a binary multiline value in python using pytest Python pytest-mock assert_has_calls - Python pytest-mock assert_has_calls Python pytest 模拟失败,function 调用断言的“断言无” - Python pytest mock fails with "assert None" for function call assertions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM