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

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

can pytest ignore a specific warning?

问题

我想让 pytest 忽略 RemovedInDjango20Warning。

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

  1. [pytest]
  2. filterwarnings =
  3. ignore:RemovedInDjango20Warning

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

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

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

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

~

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

  1. ignore:django.RemovedInDjango20Warning

以及

  1. ignore:django.utils.deprecation.RemovedInDjango20Warning

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

我收到的 INTERNAL ERROR 如下:

  1. INTERNALERROR> Traceback (most recent call last):
  2. INTERNALERROR> File "/home/chris/.pyenv/versions/3.6.10/lib/python3.6/warnings.py", line 246, in _getcategory
  3. INTERNALERROR> cat = getattr(m, klass)
  4. INTERNALERROR> AttributeError: module 'django' has no attribute 'RemovedInDjango20Warning'
  5. INTERNALERROR>
  6. INTERNALERROR> During handling of the above exception, another exception occurred:
  7. INTERNALERROR>
  8. INTERNALERROR> Traceback (most recent call last):
  9. INTERNALERROR> File "/home/chris/.virtualenvs/website-36/lib/python3.6/site-packages/_pytest/main.py", line 196, in wrap_session
  10. INTERNALERROR> session.exitstatus = doit(config, session) or 0
  11. [long traceback snipped]
  12. INTERNALERROR> raise _OptionError("unknown warning category: %r" % (category,))
  13. INTERNALERROR> warnings._OptionError: unknown warning category: 'django.RemovedInDjango20Warning'
英文:

I'd like pytest to ignore the RemovedInDjango20Warning.

I currently have this in pytest.ini

  1. [pytest]
  2. filterwarnings =
  3. 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:

  1. ignore:django.RemovedInDjango20Warning

and

  1. ignore:django.utils.deprecation.RemovedInDjango20Warning

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

The INTERNALERRORs that I get are:

  1. INTERNALERROR> Traceback (most recent call last):
  2. INTERNALERROR> File "/home/chris/.pyenv/versions/3.6.10/lib/python3.6/warnings.py", line 246, in _getcategory
  3. INTERNALERROR> cat = getattr(m, klass)
  4. INTERNALERROR> AttributeError: module 'django' has no attribute 'RemovedInDjango20Warning'
  5. INTERNALERROR>
  6. INTERNALERROR> During handling of the above exception, another exception occurred:
  7. INTERNALERROR>
  8. INTERNALERROR> Traceback (most recent call last):
  9. INTERNALERROR> File "/home/chris/.virtualenvs/website-36/lib/python3.6/site-packages/_pytest/main.py", line 196, in wrap_session
  10. INTERNALERROR> session.exitstatus = doit(config, session) or 0
  11. [long traceback snipped]
  12. INTERNALERROR> raise _OptionError("unknown warning category: %r" % (category,))
  13. INTERNALERROR> warnings._OptionError: unknown warning category: 'django.RemovedInDjango20Warning'
  14. </details>
  15. # 答案1
  16. **得分**: 46
  17. @LaurentBristiel 的评论基础上重新审视时,我意识到我需要两个冒号,再加上异常的完整路径,所以
  18. ```python
  19. [pytest]
  20. filterwarnings =
  21. 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

  1. [pytest]
  2. filterwarnings =
  3. 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:

确定