[Solved]-Pytest and Django settings runtime changes


Apparently pytest-django explicitly sets DEBUG to False (source code link).

Diving through the git history of pytest-django a bit, this was done to match Django’s default behavior (pytest commit link).

From the Django docs:

Regardless of the value of the DEBUG setting in your configuration file, all Django tests run with DEBUG=False. This is to ensure that
the observed output of your code matches what will be seen in a
production setting.

As a workaround you can use pytest-django’s settings fixture to override so DEBUG=True if you need it to be. For example,

def test_my_thing(settings):
    settings.DEBUG = True
    # ... do your test ...


For anyone who is having similar problem. I found the reason. I downloaded source files of pytest-django and found out that it sets DEBUG to False in pytest-django/pytest_django/plugin.py:338. I do not know why tho.



Add the following line in the pytest.ini file:

django_debug_mode = True

Leave a comment