【问题标题】:Mute Pytest library output for imported library为导入的库静音 Pytest 库输出
【发布时间】:2017-11-24 01:34:45
【问题描述】:

我正在测试一个使用Python Wand 的函数。当我使用无效输入对其进行测试时,它会引发 BlobError,这是 Wand 库中的自定义错误。但是,当我这样做时,似乎在 Wand 中发生了另一个 TypeError,这导致 Pytest 结果标准输出下出现了一个非常丑陋的Exception ignored in... 错误消息。

我正在检查 Pytest 是否引发了 BlobError,这很好,但我也无法检查它是否引发了 TypeError,或者只是使该消息静音。所以这里是 Pytest 的测试输出:

================================ test session starts =================================
platform linux -- Python 3.5.2, pytest-3.2.5, py-1.5.2, pluggy-0.4.0
rootdir: /path/to/my/project/tests, inifile:
collected 1 item                                                                                                                              

test_split.py .

============================== 1 passed in 0.14 seconds ==============================
Exception ignored in: <bound method Resource.__del__ of <wand.image.Image: (empty)>>
Traceback (most recent call last):
  File "/my/comp/.local/share/virtualenvs/project-BRze6wfl/lib/python3.5/site-packages/wand/resource.py", line 232, in __del__
    self.destroy()
  File "/my/comp/.local/share/virtualenvs/project-BRze6wfl/lib/python3.5/site-packages/wand/image.py", line 2767, in destroy
    for i in range(0, len(self.sequence)):
TypeError: object of type 'NoneType' has no len()

我的测试很简单:

with pytest.raises(wand.exceptions.BlobError):
        wand_split.split(PDF,TEMP, 260)

所以测试按我想要的方式进行,但是我该如何处理额外的 Wand TypeError?

【问题讨论】:

  • 我正在使用最新的 Wand 版本。甚至尝试了不同的版本,同样的问题。
  • 看一下我提到的github issue,建议的补丁还没有合并,所以你需要手动申请。
  • 是的,这个补丁解决了这个问题。我会将答案标记为正确

标签: python testing typeerror pytest wand


【解决方案1】:

该消息表明__del__doc says 内部引发了异常:

由于调用 del() 方法的不稳定环境,在其执行期间发生的异常将被忽略,而是向 sys.stderr 打印警告。 ...

这很可能发生在 python 解释器退出时,据我所知,没有简单的方法可以使用 pytest 静音此消息。

但是有了 traceback 的行号,我猜你使用的是有问题的 wand 版本,有人有 reported a similar issue on github,你可以尝试升级并在那里报告问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多