3

Python 2.7 Django 1.2

I am getting odd local_settings behavior when I am testing a Django app. I have my <project>/settings.py set up like this:

DEBUG = False
TEMPLATE_DEBUG = DEBUG

ADMINS = (
        ("Me", "me@me.com"),
)

MANAGERS = ADMINS + (('Person1', 'person1@blah.com'),)

# ... rest of settings

try:
    from local_settings import *
except ImportError:
    pass

and in <project>/local_settings.py I have:

DEBUG = True

MANAGERS = (
        ('Me', 'me@me.com'),
)

So, while working locally, the MANAGERS setting should be (('Me', 'me@me.com'),), and DEBUG should be set to True.

However, in the tests for one of my apps, I am testing settings.DEBUG and getting False, but the MANAGERS setting is set correctly (it just has 'Me' in it). Any ideas why this would happen? Here are the relevant parts of the <project>/<app>/tests.py file:

from django.conf import settings
from django.test import TestCase

# ...
class MyTests(TestCase):
    def mytest(self):
        if settings.DEBUG:
            self.assertEqual(settings.MANAGERS, (('Me', 'me@me.com'),))
        else:
            self.assertEqual(settings.MANAGERS, (('Me', 'me@me.com'), ('Person1', 'person1@blah.com')))

The result is

AssertionError: (('Me', 'me@me.com'),) != (('Me', 'me@me.com'), ('Person1', 'person1@blah.com'))

So it looks like it is testing the else branch due to settings.DEBUG being set incorrectly, and then raising AssertionError since settings.MANAGERS is set correctly.

If I run python manage.py shell I get this:

>>> from django.conf import settings
>>> settings.DEBUG
True
>>> settings.MANAGERS
(('Me', 'me@me.com'),)

So they are set correctly there.

I know I can override settings manually in my tests, but I wanted to try to use settings.DEBUG so that the test would pass no matter whether it was being run locally or in production.

Any ideas?

  • 1
    Possible duplicate of [How to Unit test with different settings in Django?](http://stackoverflow.com/questions/913549/how-to-unit-test-with-different-settings-in-django) – J. Chomel Jun 10 '16 at 07:00

3 Answers3

10

This is an intentional choice within Django:

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.

Ref: https://docs.djangoproject.com/en/dev/topics/testing/#other-test-conditions

umbrae
  • 1,101
  • 8
  • 12
  • 1
    This is NUTS. How are you supposed to debug your tests? What if you have emails only set when DEBUG is False? – MagicLAMP Jul 18 '16 at 15:31
  • @MagicLAMP You can use [override_settings](https://docs.djangoproject.com/en/1.10/topics/testing/tools/#django.test.override_settings) in that case – noisecapella Oct 25 '16 at 17:47
3

Use --settings option when running tests

python manage.py test --settings=mysite.settings_local
MicroPyramid
  • 1,570
  • 19
  • 22
1

I recall reading that you're not supposed to change settings values in any way at runtime, it causes problems.

Gabriel Ross
  • 5,168
  • 1
  • 28
  • 30
  • 1
    The django documentation says not to do this: `settings.BLAH = 'blah'` outside of a settings file but importing a file into a settings file is kosher –  Aug 26 '11 at 17:09