python - 为导入的库静音 Pytest 库输出

标签 python testing typeerror pytest wand

我正在测试一个使用 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?

最佳答案

该消息表明在 __del__ 中引发了异常,doc says :

Due to the precarious circumstances under which del() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead. ...

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

但是使用 traceback 的行号,我猜你使用的是有问题的魔杖版本,有人有 reported a similar issue on github ,您可以尝试升级并在此处报告问题。

关于python - 为导入的库静音 Pytest 库输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47465218/

相关文章:

python - 为什么 exec() 中的符号定义语句有时对本地符号表没有影响?

python - 如何在python中动态更新字典

python - 在python 3中将表情符号转换为Unicode,反之亦然

java - 如何在不初始化类的情况下模拟内部成员

testing - 在数据流覆盖中,返回一个变量是否使用它?

python -/admin/ 'set' 对象的类型错误是不可逆的,并且要反转的参数 ( ) 必须是一个序列

python - 我应该如何设置版本之间的版本号?

javascript - Jest : Unit test toHaveBeenCalled() not working

javascript - Nodejs 比较两个不同的 Excel 文件并发现它们的差异

python - 正常列表 'type' 对象不可订阅