我经常从我无法控制的库中得到很多抨击,我不想用它们污染测试执行。
如何避免这种情况,而不冒着禁用自己代码中的抨击的风险?
例子:

================================================================================ 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:

全部忽略
[pytest]
filterwarnings =
    ignore::DeprecationWarning

忽略消息中包含DeprecationWarning的所有DeprecationWarnings
[pytest]
filterwarnings =
    ignore:.*invalid escape sequence.*:DeprecationWarning

仅在invalid escape sequence模块中忽略DeprecationWarnings
[pytest]
filterwarnings =
    ignore::DeprecationWarning:toml.decoder

忽略第47行toml.decoder模块中的DeprecationWarnings
[pytest]
filterwarnings =
    ignore::DeprecationWarning:toml.decoder:47

仅在toml.decoder模块中忽略DeprecationWarnings,仅在第47行,且消息中只有toml.decoder
[pytest]
filterwarnings =
    ignore:.*invalid escape sequence.*:DeprecationWarning:toml.decoder:47

10-07 17:52