TestSettingsManager: temporarily change settings for tests

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()

More like this

  1. Dynamic thumbnail generator by semente 5 years, 2 months ago
  2. SuperChoices by willhardy 4 years, 6 months ago
  3. RedirectedURLField by stringify 3 years, 3 months ago
  4. Hidden Forms by insin 5 years, 10 months ago
  5. Counter model - run multiple persistent counters by simon 3 years, 11 months ago

Comments

carljm (on August 30, 2008):

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?

#

reanes (on September 23, 2008):

What I do is define a new test_settings.py file that looks like:

from settings import *
# add any additional or overridden settings and here
# you could add your automated syncdb stuff in here as well...

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.

#

bob84123 (on December 18, 2008):

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.

#

carljm (on February 3, 2009):

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.

#

muhuk (on October 7, 2009):

@carljim: it complains that ImproperlyConfigured: App with label testapp could not be found when I modify INSTALLED_APPS (Django 1.1). syncdb below seems to work:

def syncdb(self):
    loading.cache.loaded = False
    loading.cache.app_store = SortedDict()
    loading.cache.app_models = SortedDict()
    loading.cache.app_errors = {}
    loading.cache.handled = {}
    loading.cache.postponed = []
    call_command('syncdb', verbosity=0)

#

muhuk (on October 7, 2009):

@carljim: I guess I was wrong, syncdb method was working just fine. But I had to subclass TransactionTestCase in order to get tables for new models created.

#

rix (on December 8, 2009):

Really nice, very useful snippet! Thanks!

#

lowik (on December 16, 2009):

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:

    delattr(settings, k)

except AttributeError:

    delattr(settings._wrapped, k)

#

akaihola (on August 19, 2010):

See also a solution for temporarily modifying settings inside an individual test in snippet 2156.

#

carljm (on October 5, 2010):

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.

#

tobia (on November 5, 2012):

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≫.

#

vdboor (on November 8, 2012):

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

#

(Forgotten your password?)