我想将 pytest aserts 的结果放入日志中。
首先我尝试了这个解决方案
def logged_assert(self, testval, msg=None):
if not testval:
if msg is None:
try:
assert testval
except AssertionError as e:
self.logger.exception(e)
raise e
self.logger.error(msg)
assert testval, msg
它工作正常,但如果内置,我需要为每个断言使用我自己的 msg。问题是 testval 在传递给函数时进行评估,错误 msg 是
AssertionError: False
我找到了解决问题的好方法http://code.activestate.com/recipes/577074-logging-asserts/这里是第一条评论。
我在我的记录器包装器模块中写了这个函数
def logged_excepthook(er_type, value, trace):
print('HOOK!')
if isinstance(er_type, AssertionError):
current = sys.modules[sys._getframe(1).f_globals['__name__']]
if 'logger' in sys.modules[current]:
sys.__excepthook__(er_type, value, trace)
sys.modules[current].error(exc_info=(er_type, value, trace))
else:
sys.__excepthook__(er_type, value, trace)
else:
sys.__excepthook__(er_type, value, trace)
然后
sys.excepthook = logged_excepthook
在测试模块中,我断言了
的输出import sys
print(sys.excepthook, sys.__excepthook__, logged_excepthook)
是
<function logged_excepthook at 0x02D672B8> <built-in function excepthook> <function logged_excepthook at 0x02D672B8>
但是我的输出中没有“ Hook ”消息。而且我的日志文件中也没有错误消息。所有的工作都像内置的 sys.excepthook。
我查看了 pytest 源代码,但 sys.excepthook 在那里没有改变。 但是,如果我使用 Cntrl-C 中断我的代码执行,我会在标准输出中收到“Hook”消息。
主要问题是为什么内置的 sys.excepthook 调用了我的自定义函数而不是我的自定义函数,我该如何解决这个问题。 但如果存在另一种记录断言错误的方法,我也很感兴趣。
我在 64 位 Windows 8.1 上使用 python3.2(32 位)。
最佳答案
excepthook
仅在存在未处理 异常时触发,即正常终止程序的异常。测试中的任何异常都由测试框架处理。
参见 Asserting with the assert
statement - pytest documentation关于如何使用该功能。自定义消息以标准方式指定:assert condition, failure_message
。
如果您对 pytest
处理 assert
的方式不满意,您需要要么
- 使用包装器,或者
- hook the
assert
statement .
pytest
也使用 assert
Hook 。它的逻辑位于 Lib\site-packages\_pytest\assertion
(stock plugin)。在其中包装/替换一些功能可能就足够了。为避免修补代码库,您可以使用自己的插件:在运行时修补 exceptions
插件,或者
禁用它并自己重新使用它的功能。
关于python - 自定义 sys.excepthook 不适用于 pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29036354/