Reputation:
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", "[email protected]"),
)
MANAGERS = ADMINS + (('Person1', '[email protected]'),)
# ... rest of settings
try:
from local_settings import *
except ImportError:
pass
and in <project>/local_settings.py
I have:
DEBUG = True
MANAGERS = (
('Me', '[email protected]'),
)
So, while working locally, the MANAGERS
setting should be (('Me', '[email protected]'),)
, 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', '[email protected]'),))
else:
self.assertEqual(settings.MANAGERS, (('Me', '[email protected]'), ('Person1', '[email protected]')))
The result is
AssertionError: (('Me', '[email protected]'),) != (('Me', '[email protected]'), ('Person1', '[email protected]'))
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', '[email protected]'),)
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?
Upvotes: 3
Views: 3640
Reputation: 1630
Use --settings
option when running tests
python manage.py test --settings=mysite.settings_local
Upvotes: 3
Reputation: 1129
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
Upvotes: 10
Reputation: 5198
I recall reading that you're not supposed to change settings values in any way at runtime, it causes problems.
Upvotes: 1