根据医生们,您可以忽略这样的警告:
@pytest.mark.filterwarnings("ignore:api v1")
def test_foo():这意味着:
但是似乎没有任何关于这种微型语言的文档(它甚至是一种小型语言吗?)
比赛进行得如何?
我这样问是因为下面的测试不会忽略通过导入DeprecationWarning引发的boto3
@pytest.mark.filterwarnings("ignore:DeprecationWarning")
def test_ignore_warnings():
import boto3Pytest产出:
============================================================================================================================== warnings summary ===============================================================================================================================
/home/rob/dev/time-series/.venv/lib/python3.7/site-packages/botocore/awsrequest.py:624
/home/rob/dev/time-series/.venv/lib/python3.7/site-packages/botocore/awsrequest.py:624: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
class HeadersDict(collections.MutableMapping):
-- Docs: https://docs.pytest.org/en/latest/warnings.html
==================================================================================================================== 1 passed, 1 warnings in 0.36 seconds =====================================================================================================================发布于 2019-10-31 14:52:20
当您在-W参数中使用python命令时,过滤器的工作方式相同(参见python --help)。格式在warnings模块的文档中进行了描述。简而言之,它是action:message:category:module:line,action可能是强制性的,但其他部分可以省略。
"ignore:api v1"将试图通过定义“包含警告消息的开始必须匹配的正则表达式的字符串”来匹配message。因为您实际上想要匹配category,所以可以跳过message。这意味着您在ignore之后似乎缺少了一个冒号,所以这是正确的格式:
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_ignore_warnings():
import boto3但是,如果在测试函数之外导入包时发生了这种情况,那么显然仍然会收到警告。在这种情况下,您可能需要全局指定过滤器作为pytest的参数:
pytest -W "ignore::DeprecationWarning" ./tests/
...or将它添加到pytest.ini中
[pytest]
filterwarnings =
ignore::DeprecationWarning如果这种全局排除是不可取的,则可以尝试将其限制在特定模块上:
ignore::DeprecationWarning:boto3
测试
出于测试目的,您可以使用以下代码:
import warnings
def something():
warnings.warn("Test", DeprecationWarning)
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_ignore_warnings():
something()https://stackoverflow.com/questions/58645563
复制相似问题