pytest 可以忽略特定警告吗?

Chr*_*vey 14 python pytest

我希望 pytest 忽略 RemovedInDjango20Warning。

我目前在 pytest.ini 中有这个

[pytest]
filterwarnings =
    ignore:RemovedInDjango20Warning
Run Code Online (Sandbox Code Playgroud)

这允许 pytest 运行,但不会抑制警告。

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

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

我害怕添加第四个冒号:)

~

我还尝试更具体地了解错误:

ignore:django.RemovedInDjango20Warning
Run Code Online (Sandbox Code Playgroud)

ignore:django.utils.deprecation.RemovedInDjango20Warning
Run Code Online (Sandbox Code Playgroud)

当那些运行时,我仍然每次都收到警告。

我得到的内部错误是:

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'

Run Code Online (Sandbox Code Playgroud)

Chr*_*vey 24

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

[pytest]
filterwarnings =
    ignore::django.utils.deprecation.RemovedInDjango20Warning
Run Code Online (Sandbox Code Playgroud)

谢谢大家!

  • 谢谢!我必须强调异常的**完整路径**的重要性。这对于标准异常(例如“FutureWarning”等)来说不是必需的,但对于自定义警告(例如在我的情况下来自pandas的“SettingWithCopyWarning”)是必需的。 (4认同)