自定义 sys.excepthook 不适用于 pytest



我想把pytest aserts的结果放到log中。

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

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>

但是我的输出中没有"钩子"消息。而且我的日志文件中也没有错误消息。所有工作方式都像内置的sys.excepthook一样。

我查看了pytest源代码,但sys.excepthook在那里没有改变。但是,如果我使用 Cntrl-C 中断我的代码执行,我会在标准输出中收到"钩子"消息。

主要问题是为什么内置 sys.excepthook 调用 isntead 我的自定义函数以及如何解决这个问题。但是,如果存在另一种记录断言错误的方法,我也感到好奇。

我在 3.2 位窗口 8.1 上使用 python3.2(84 位)。

只有当存在未经处理的异常(即通常终止程序的异常)时,才会触发excepthook。测试中的任何异常都由测试框架处理。

请参阅使用 assert 语句断言 - pytest 文档,了解如何使用该功能。自定义消息以标准方式指定:assert condition, failure_message

如果您对pytest处理assert的方式不满意,则需要

  • 使用包装器,或
  • 钩住assert语句。

pytest也使用assert钩。它的逻辑位于Libsite-packages_pytestassertion(一个股票插件)中。包装/替换其中的一些函数可能就足够了。为了避免修补代码库,您可以使用自己的插件:在运行时修补exceptions插件,或者禁用它并自己重用其功能。

相关内容

最新更新