如何避免使用 pytest 从内部依赖项中获取弃用警告?



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

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

例:

================================================================================ 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关于警告捕获的一般主题的文档:警告捕获。从这里,您可以缩小由更严格的过滤器捕获的警告范围。过滤器格式为

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

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

忽略一切

[pytest]
filterwarnings =
ignore:

忽略所有DeprecationWarning

[pytest]
filterwarnings =
ignore::DeprecationWarning

忽略消息中带有invalid escape sequence的所有DeprecationWarning

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

仅在toml.decoder模块中忽略DeprecationWarnings

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

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

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

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

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

相关内容

最新更新