Python pytest能否忽略特定的警告?

Python pytest能否忽略特定的警告?,python,pytest,Python,Pytest,我想让pytest忽略RemovedIndjango20警告 我目前在pytest.ini中有这个 [pytest] filterwarnings = ignore:RemovedInDjango20Warning 这允许pytest运行,但不会抑制警告 如果我添加第二个冒号,我会收到很多“内部错误”消息 如果添加第三个冒号,测试将运行,但不会抑制警告 我恐怕要加上第四个冒号:) ~ 我还尝试更具体地说明错误: ignore:django.RemovedInDjango20Warnin

我想让pytest忽略RemovedIndjango20警告

我目前在pytest.ini中有这个

[pytest]
filterwarnings =
    ignore:RemovedInDjango20Warning
这允许pytest运行,但不会抑制警告

如果我添加第二个冒号,我会收到很多“内部错误”消息

如果添加第三个冒号,测试将运行,但不会抑制警告

我恐怕要加上第四个冒号:)

~

我还尝试更具体地说明错误:

ignore:django.RemovedInDjango20Warning

当它们运行时,我仍然每次都被警告

我得到的内部错误有:

INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/home/chris/.pyenv/versions/3.6.10/lib/python3.6/warnings.py", line 246, in _getcategory
INTERNALERROR>     cat = getattr(m, klass)
INTERNALERROR> AttributeError: module 'django' has no attribute 'RemovedInDjango20Warning'
INTERNALERROR> 
INTERNALERROR> During handling of the above exception, another exception occurred:
INTERNALERROR> 
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/home/chris/.virtualenvs/website-36/lib/python3.6/site-packages/_pytest/main.py", line 196, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0

[long traceback snipped]

INTERNALERROR>     raise _OptionError("unknown warning category: %r" % (category,))
INTERNALERROR> warnings._OptionError: unknown warning category: 'django.RemovedInDjango20Warning'


根据@LaurentBristiel的评论再看一眼,我意识到我需要两个冒号,加上异常的完整路径,所以

[pytest]
filterwarnings =
    ignore::django.utils.deprecation.RemovedInDjango20Warning

谢谢大家

您所拥有的看起来是正确的…根据文档()正确的语法似乎是两个冒号(::)(而不是“逗号”)。在这种情况下,你能分享你收到的“内部错误”信息吗?@LaurentBristiel关于冒号和逗号的说法你是对的——我写文章时很草率,我会更新的。谢谢,谢谢!我必须强调,通往例外的完整路径的重要性。这对于标准异常(如
FutureWarning
等)不是必需的,但对于自定义警告(如在我的例子中,pandas的
setingwithcopywarning
)是必需的。
[pytest]
filterwarnings =
    ignore::django.utils.deprecation.RemovedInDjango20Warning