How to suppress the `RemovedInDjango40Warning` warnings in a unit test?

We are working on a unit test in PyCharm and the test itself is passing now. However, the terminal output contains warning messages in the RemovedInDjango40Warning category. As we do not plan to upgrade to Django 4.0 for now, these warnings are verbose and distracting.

We tried including the statement warnings.simplefilter("ignore", category=RemovedInDjango40Warning) but it did not suppress the warnings.

So, we wonder how to suppress the RemovedInDjango40Warning warnings.

Technical Details:

Partial source code of the unit test:
from unittest import TestCase

import Django


class TestPocFunctional(TestCase):
    @classmethod
    def setUpClass(cls):
        django.setup()
        return

    def test_all(self):
        from application.poc import PocFunctional
        import warnings
        from django.utils.deprecation import RemovedInDjango40Warning
        warnings.simplefilter("ignore", category=RemovedInDjango40Warning)
        
        # ... testing steps
        
        return

The terminal output of warnings:
/data/app-py3/venv3.7/bin/python /var/lib/snapd/snap/pycharm-professional/316/plugins/python/helpers/pycharm/_jb_unittest_runner.py --target test_poc.TestPocFunctional 
Testing started at 1:54 PM ...
Launching unittests with arguments python -m unittest test_poc.TestPocFunctional in /data/app-py3/APPLICATION/tests

/data/app-py3/APPLICATION/eav/models.py:84: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  value = models.CharField(_(u"value"), db_index=True,
/data/app-py3/APPLICATION/eav/models.py:100: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  name = models.CharField(_(u"name"), unique=True, max_length=100)
/data/app-py3/APPLICATION/eav/models.py:102: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  enums = models.ManyToManyField(EnumValue, verbose_name=_(u"enum group"))
/data/app-py3/APPLICATION/eav/models.py:173: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  (TYPE_TEXT, _(u"Text")),
/data/app-py3/APPLICATION/eav/models.py:174: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  (TYPE_FLOAT, _(u"Float")),
/data/app-py3/APPLICATION/eav/models.py:175: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  (TYPE_INT, _(u"Integer")),
/data/app-py3/APPLICATION/eav/models.py:176: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  (TYPE_DATE, _(u"Date and Time")),
/data/app-py3/APPLICATION/eav/models.py:177: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  (TYPE_BASICDATE, _(u"Date")),
/data/app-py3/APPLICATION/eav/models.py:178: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  (TYPE_BOOLEAN, _(u"True / False")),
/data/app-py3/APPLICATION/eav/models.py:179: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  (TYPE_OBJECT, _(u"Django Object")),
/data/app-py3/APPLICATION/eav/models.py:180: RemovedInDjango40Warning: django.utils.translation.ugettext_lazy() is deprecated in favor of django.utils.translation.gettext_lazy().
  (TYPE_ENUM, _(u"Multiple Choice")),

... more warnings in the same category ...

I might try making these a “global” setting by either putting them outside the class in that module or making them entries in your settings.py file.

1 Like

Changed the unit test to the sample code below, and unfortunately, the warnings are still coming out in the terminal output.

So, we are still looking forward to more hints and suggestions.

from unittest import TestCase

import Django

import warnings
from django.utils.deprecation import RemovedInDjango40Warning
warnings.simplefilter("ignore", category=RemovedInDjango40Warning)


class TestPocFunctional(TestCase):
    @classmethod
    def setUpClass(cls):
        django.setup()
        return

    def test_all(self):
        from application.poc import PocFunctional
        import warnings
        from django.utils.deprecation import RemovedInDjango40Warning
        warnings.simplefilter("ignore", category=RemovedInDjango40Warning)
        
        # ... testing steps
        
        return