pytest - 抑制来自特定 3rd 方模块的 DeprecationWarning

Whi*_*ger 21 python pytest

当我运行 pytest 时,我收到了来自 3rd 方库的一些弃用警告。我想知道我自己代码中的任何弃用警告,而不是与另一个 3rd 方库捆绑在一起的库的供应商副本。

这个答案有助于让我中途到达那里。如果我像这样运行 pytest: $ pytest ./tests/我得到:

$ pytest ./tests/
============================= test session starts ==============================
platform linux -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: /home/whlt/repos/tj-image-resizer/tests, inifile: pytest.ini
collected 5 items                                                              

tests/test_file1.py .                                                   [ 20%]
tests/test_file2.py ....                                                [100%]

=============================== warnings summary ===============================
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
  /home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1: 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, MutableMapping

-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================== 5 passed, 2 warnings in 2.54s =========================
Run Code Online (Sandbox Code Playgroud)

但是如果我像这样运行 pytest:$ pytest ./tests/ -W ignore::DeprecationWarning我得到:

============================= test session starts ==============================
platform linux -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: /home/whlt/repos/tj-image-resizer/tests, inifile: pytest.ini
collected 5 items                                                              

tests/test_file1.py .                                                   [ 20%]
tests/test_file2.py ....                                                [100%]

============================== 5 passed in 2.61s ===============================
Run Code Online (Sandbox Code Playgroud)

第二个输出显示过滤器有效,但这也会隐藏我希望看到的由我自己的代码产生的任何弃用警告。

这个问题的一部分是我不确定在忽略过滤器中尝试引用哪个模块。我试过了$ pytest ./tests/ -W ignore::DeprecationWarning:urllib3.*:,我也试过了$ pytest ./tests/ -W ignore::DeprecationWarning:botocore.*:。这两个结果都与第一个没有过滤的示例产生相同的输出。

我怎样才能从版本滤除DeprecationWarnings的urllib3用的vendored版包装requests附带的botocore(当我运行与命令都会调用boto3库)?

San*_*ños 19

您应该使用警告过滤器选项(ini 或标记):

[pytest]
filterwarnings =
    ignore::DeprecationWarning:botocore.*:
Run Code Online (Sandbox Code Playgroud)

来源:https : //docs.python.org/3/library/warnings.html#default-warning-filter

“个别警告过滤器被指定为由冒号分隔的字段序列:”

action:message:category:module:line
Run Code Online (Sandbox Code Playgroud)

  • @WhiteHotLoveTiger 编辑。另外,看到您对引用的问题发表了评论。有效吗? (2认同)
  • 我应该将第一个代码片段的内容放在哪里?我将其放在“pytest.ini”中,在我的“*.py”文件旁边,用“python3 -m pytest *.py”调用,并且警告仍然存在。 (2认同)

Vla*_*den 6

如果您使用pyproject.toml文件进行 pytest 配置,您可以使用:

 [tool.pytest.ini_options]
 testpaths = ["./tests/unit"]
 filterwarnings = ["ignore:::.*third_party_package.module:123", "ignore:::.*another_module*"]
Run Code Online (Sandbox Code Playgroud)

ignore:::.*third_party_package.module:123忽略特定行的特定警告 ignore:::.*another_module*忽略模块中的所有警告。

请注意,您可以有多个忽略。您需要将它们列出在[]中。另外,你不能让*third_party_package/module:123你必须全部替换/.