【问题标题】:custom sys.excepthook doesn't work with pytest自定义 sys.excepthook 不适用于 pytest
【发布时间】:2015-05-16 04:16:53
【问题描述】:

我想将 pytest 断言的结果放入日志中。

首先我尝试了这个解决方案

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 位)。

【问题讨论】:

    标签: python pytest


    【解决方案1】:

    excepthook 仅在存在 未处理 异常时触发,即正常终止程序的异常。测试中的任何异常都由测试框架处理。

    请参阅Asserting with the assert statement - pytest documentation,了解如何使用该功能。自定义消息以标准方式指定:assert condition, failure_message

    如果您对pytest 处理asserts 的方式不满意,您需要这样做

    pytest 也使用 assert 钩子。它的逻辑位于Lib\site-packages\_pytest\assertion(一个stock plugin)。在那里包装/替换一些功能可能就足够了。为避免修补代码库,您可以使用自己的插件:在运行时修补 exceptions 插件,或 禁用它并自己重用其功能。

    【讨论】:

    • 谢谢!你的回答让我清醒了。我只是使用 'pytest_assertrepr_compare' 来添加日志记录。由于某些原因,这不是一个理想的解决方案,但我可以接受。 util.assertrepr_compare 没有返回问题(这不是一个真正的问题 - 如果没有默认解释,我可以定义自己的简单解释)。第二个是我无法将味精添加到日志中(assert testval, msg)。但这也不是问题,我可以在这种情况下使用我的logged_assert 方法。
    猜你喜欢
    • 1970-01-01
    • 2016-04-29
    • 2017-06-25
    • 2019-05-18
    • 2013-11-05
    • 2020-12-05
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多