gpt4 book ai didi

python - 如何避免使用 pytest 从内部依赖项中获取 DeprecationWarning?

转载 作者:行者123 更新时间:2023-11-28 22:09:02 27 4
gpt4 key购买 nike

我经常从我无法控制的库中得到很多弃用,我不想用它们污染测试执行。

如何在不冒从我自己的代码中禁用弃用的风险的情况下避免这种情况?

例子:

================================================================================ warnings summary ==================================================================================
.tox/py27-ansible25-unit/lib/python3.6/site-packages/toml/decoder.py:47
/Users/ssbarnea/os/molecule/.tox/py27-ansible25-unit/lib/python3.6/site-packages/toml/decoder.py:47: DeprecationWarning: invalid escape sequence \.
TIME_RE = re.compile("([0-9]{2}):([0-9]{2}):([0-9]{2})(\.([0-9]{3,6}))?")

.tox/py27-ansible25-unit/lib/python3.6/site-packages/sh.py:424
/Users/ssbarnea/os/molecule/.tox/py27-ansible25-unit/lib/python3.6/site-packages/sh.py:424: DeprecationWarning: invalid escape sequence \d
rc_exc_regex = re.compile("(ErrorReturnCode|SignalException)_((\d+)|SIG[a-zA-Z]+)")

.tox/py27-ansible25-unit/lib/python3.6/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:152
/Users/ssbarnea/os/molecule/.tox/py27-ansible25-unit/lib/python3.6/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:152: DeprecationWarning: invalid escape sequence \*

最佳答案

我不会重复关于警告捕获的一般主题的 pytest 文档,以供引用:Warnings Capture .从这里,您可以缩小由更严格的过滤器捕获的警告。 filter format

{action}:{message}:{category}:{module}:{lineno}

带有可跳过的元素。要粘贴到 pytest.ini 中的示例,从一般到具体:

忽略一切

[pytest]
filterwarnings =
ignore:

忽略所有DeprecationWarning

[pytest]
filterwarnings =
ignore::DeprecationWarning

忽略消息中所有带有无效转义序列DeprecationWarning

[pytest]
filterwarnings =
ignore:.*invalid escape sequence.*:DeprecationWarning

仅在 toml.decoder 模块中忽略 DeprecationWarning

[pytest]
filterwarnings =
ignore::DeprecationWarning:toml.decoder

仅在第 47 行的 toml.decoder 模块中忽略 DeprecationWarning

[pytest]
filterwarnings =
ignore::DeprecationWarning:toml.decoder:47

仅在 toml.decoder 模块中忽略 DeprecationWarning,仅在第 47 行且仅在消息中包含 invalid escape sequence:

[pytest]
filterwarnings =
ignore:.*invalid escape sequence.*:DeprecationWarning:toml.decoder:47

关于python - 如何避免使用 pytest 从内部依赖项中获取 DeprecationWarning?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57925071/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com