[英]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.