如何忽略 pytest 中有关 Django41 中删除的警告?

Kim*_*cks 1 python django pytest

https://docs.pytest.org/en/6.2.x/warnings.html#deprecationwarning-and-pendingdeprecationwarning教我在 pytest.ini 中使用它

filterwarnings = ignore:.*U.*mode is deprecated:DeprecationWarning

但我仍然收到这样的警告:

../usr/local/lib/python3.8/dist-packages/django/apps/registry.py:91
  /usr/local/lib/python3.8/dist-packages/django/apps/registry.py:91: RemovedInDjango41Warning: 'pattern_library' defines default_app_config = 'pattern_library.apps.PatternLibraryAppConfig'. Django now detects this configuration automatically. You can remove default_app_config.
    app_config = AppConfig.create(entry)
Run Code Online (Sandbox Code Playgroud)

我也想抑制这一点,因为这也是我无法控制的第三方库,尽管我已经提交了 PR 来更新它。

我应该在 pytest.ini 中添加什么?

Mar*_*ers 5

您可以在给定警告类别的完整导入路径上注册过滤器;这是django.utils.deprecation.RemovedInDjango41Warning

filterwarnings =
    ignore:.*U.*mode is deprecated:DeprecationWarning
    ignore:.*Django now detects this configuration.*:django.utils.deprecation.RemovedInDjango41Warning
Run Code Online (Sandbox Code Playgroud)

根据您的 Django 版本,RemovedInDjango41WarningDeprecationWarning(Django 4.0,仍在开发中)或PendingDeprecationWarning(Django 3.2)的子类,并且警告过滤器也与子类匹配,因此:

ignore:.*Django now detects this configuration.*:PendingDeprecationWarning
ignore:.*Django now detects this configuration.*:DeprecationWarning
Run Code Online (Sandbox Code Playgroud)

对于 Django 3.2 和 4.0 也适用。