from django.conf import settings
from django.core.management import call_command
from django.db.models import loading
from django.test import TestCase
NO_SETTING = ('!', None)
class TestSettingsManager(object):
"""
A class which can modify some Django settings temporarily for a
test and then revert them to their original values later.
Automatically handles resyncing the DB if INSTALLED_APPS is
modified.
"""
def __init__(self):
self._original_settings = {}
def set(self, **kwargs):
for k,v in kwargs.iteritems():
self._original_settings.setdefault(k, getattr(settings, k,
NO_SETTING))
setattr(settings, k, v)
if 'INSTALLED_APPS' in kwargs:
self.syncdb()
def syncdb(self):
loading.cache.loaded = False
call_command('syncdb', verbosity=0)
def revert(self):
for k,v in self._original_settings.iteritems():
if v == NO_SETTING:
delattr(settings, k)
else:
setattr(settings, k, v)
if 'INSTALLED_APPS' in self._original_settings:
self.syncdb()
self._original_settings = {}
class SettingsTestCase(TestCase):
"""
A subclass of the Django TestCase with a settings_manager
attribute which is an instance of TestSettingsManager.
Comes with a tearDown() method that calls
self.settings_manager.revert().
"""
def __init__(self, *args, **kwargs):
super(SettingsTestCase, self).__init__(*args, **kwargs)
self.settings_manager = TestSettingsManager()
def tearDown(self):
self.settings_manager.revert()
Comments
I kept having the feeling as I was writing this code that there MUST be some already-existing way to do this that I'm just too blind to see. Is there a better pattern for this that I'm missing?
#
What I do is define a new test_settings.py file that looks like:
Then copy your manage.py to manage_test.py and change the line: "import settings" to "import test_settings as settings"
run manage_test.py instead of manage.py whenever you want to use your test settings.
#
reanes's suggestion is great. Just remember to use
from django.conf import settings
rather than 'import settings' in your code that reads the settings, otherwise it'll just read the normal settings.py anyway.
#
Hmm, that solution doesn't seem like it would cover my use cases. I often have particular apps that need particular test settings (or additional test models), and it doesn't make sense to include those in the test settings for every single app.
#
@carljim: it complains that
ImproperlyConfigured: App with label testapp could not be foundwhen I modify INSTALLED_APPS (Django 1.1).syncdbbelow seems to work:#
@carljim: I guess I was wrong,
syncdbmethod was working just fine. But I had to subclassTransactionTestCasein order to get tables for new models created.#
Really nice, very useful snippet! Thanks!
#
I get an AttributeError on line 35 with my django - (1, 1, 1, 'final', 0). I also see that now - http://code.djangoproject.com/svn/django/trunk/django/utils/functional.py LazyObject has [HTML_REMOVED][HTML_REMOVED]delattr[HTML_REMOVED][HTML_REMOVED] which is missing in my local version and with that everything works ok. This workaround fix my problem:
try:
except AttributeError:
#
See also a solution for temporarily modifying settings inside an individual test in snippet 2156.
#
Note that the syncdb portion of this will only work in Django 1.1+ if you subclass from TransactionTestCase, which will slow your tests significantly. I don't really recommend the use of this snippet anymore.
#
Nice, thanks.
A note about NO_SETTING: setting it to something unlikely and then using == for comparison is less than ideal, because the unlikely value ('!', None) could be used in some remote setting.
A better approach is to create a new object for it, for example NO_SETTING = object(), and then using ≪is≫ for comparison. This is cleaner and safer, because Python will make sure no other value will ever evaluate true for ≪x is NO_SETTING≫.
#
This is an awesome snippet, thanks!
I've created a new version of it that's build with a Django 1.4 style in mind: http://djangosnippets.org/snippets/2843/ It uses the
override_settings()feature of Django 1.4#