django-4.0

Django 4: settings.DEBUG not correct when running tests


I am trying to write a Django (4) test. It has to work differently depending on the DEBUG constant in my settings.py. So I started with something like:

def my_test(self):
    from django.conf import settings

    if settings.DEBUG:
    ....
    else:
    ....

I noticed the code never executed the first part of the if so I added:

print(settings.DEBUG)

And I noticed this prints always False. Even if DEBUG is set to True in my settings.py.

I tried with some others constants there (INSTALLED_APPS, ALLOWED_HOSTS) and all of them return the correct value.

Why is that? How to access the real DEBUG constant in my settings.py?


Solution

  • Adding --debug-mode flag solved the issue:

    python manage.py test --debug-mode

    --debug-mode
    
    Sets the DEBUG setting to True prior to running tests. This may help troubleshoot test failures.