pytest可以忽略特定的警告吗?

huangapple go评论84阅读模式
英文:

can pytest ignore a specific warning?

问题

我想让 pytest 忽略 RemovedInDjango20Warning。

我目前在 pytest.ini 中有这个配置:

[pytest]
filterwarnings =
    ignore:RemovedInDjango20Warning

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

如果我添加第二个冒号,我会收到很多 "INTERNAL ERROR" 消息。

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

我害怕添加第四个冒号 pytest可以忽略特定的警告吗?

~

我也尝试更加具体地指定错误:

ignore:django.RemovedInDjango20Warning

以及

ignore:django.utils.deprecation.RemovedInDjango20Warning

尽管这些可以运行,但我仍然会在每次警告时收到警告。

我收到的 INTERNAL ERROR 如下:

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'
英文:

I'd like pytest to ignore the RemovedInDjango20Warning.

I currently have this in pytest.ini

[pytest]
filterwarnings =
    ignore:RemovedInDjango20Warning

And that allows pytest to run, but does not suppress the warnings.

If I add a second colon, I get a lot of "INTERNAL ERROR" messages.

If I add a third colon, the tests run, but do not suppress the warnings.

I am afraid to add a fourth colon pytest可以忽略特定的警告吗?

~

I've also tried being more specific about the error:

ignore:django.RemovedInDjango20Warning

and

ignore:django.utils.deprecation.RemovedInDjango20Warning

and while those run, I'm still warned every time.

The INTERNALERRORs that I get are:

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'


</details>


# 答案1
**得分**: 46

在 @LaurentBristiel 的评论基础上重新审视时,我意识到我需要两个冒号,再加上异常的完整路径,所以

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

感谢所有人!

英文:

While taking a second look based on @LaurentBristiel 's comments, I realized that I need two colons, PLUS the full path to the exception, so

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

Thanks to all!

huangapple
  • 本文由 发表于 2020年1月7日 02:48:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定