【问题标题】:Python pytest warnings when running test运行测试时的 Python pytest 警告
【发布时间】:2019-11-04 16:10:57
【问题描述】:

我有一个包含我的函数的 myfun.py 文件。
我有一个带有pytest 测试的test_myfun.py 文件。

当运行pytest test_myfun.py 时,测试本身运行没有错误,但 pytest 会给出警告。

有什么建议可以解决这些警告吗?

=============================== warnings summary ===============================
/home/rene/anaconda3/lib/python3.7/site-packages/html5lib/_trie/_base.py:3
  /home/rene/anaconda3/lib/python3.7/site-packages/html5lib/_trie/_base.py:3: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
    from collections import Mapping

/home/rene/anaconda3/lib/python3.7/site-packages/scrapy/item.py:8
  /home/rene/anaconda3/lib/python3.7/site-packages/scrapy/item.py:8: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
    from collections import MutableMapping

-- Docs: https://docs.pytest.org/en/latest/warnings.html
==================== 11 passed, 2 warnings in 7.47 seconds =====================

版本信息:
- Python 3.7.3
- pytest 4.6.2

# packages in environment at /home/rene/anaconda3:
#
# Name                    Version                   Build  Channel
pytest                    4.6.2                    py37_0  
pytest-arraydiff          0.3              py37h39e3cac_0  
pytest-astropy            0.5.0                    py37_0  
pytest-doctestplus        0.3.0                    py37_0  
pytest-openfiles          0.3.2                    py37_0  
pytest-remotedata         0.3.1                    py37_0  
pytest-runner             4.4                        py_0  

【问题讨论】:

  • 那些包需要更新,仅此而已。它们仍然兼容,但在 3.8 中将不再兼容,这就是消息所说的
  • 谢谢,但我从 Anaconda 安装了最新的软件包,但仍然收到这些警告。
  • 是的,正是因为作者还没有修复警告。当我说“更新”时,我的意思是“由修复代码的人更新”,而不是使用“pip”升级的人,因为升级还不存在。

标签: python warnings pytest


【解决方案1】:

你有两个选择。

1) 升级您的软件包,如果最新版本的 html5lib 和 scrapy 已修复这些警告。

2) 在你的 repo 的根目录下创建一个pytest.ini 文件并为其提供这些内容,这将使pytest 忽略DeprecationWarning

[pytest]
filterwarnings =
    ignore::DeprecationWarning

【讨论】:

  • 我对第二个选项很感兴趣(在我的例子中,最新版本的软件包会生成相同的警告)。但是,如果我的代码中有其他弃用,设置 ignore::DeprecationWarning 选项是否也会使这些警告静音?这就是我想要避免的。
猜你喜欢
  • 2023-02-11
  • 2018-01-25
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-25
相关资源
最近更新 更多