Override Django cache settings in tests Override Django cache settings in tests django django

Override Django cache settings in tests


Take a look at https://docs.djangoproject.com/en/1.8/topics/testing/tools/#overriding-settingsYou can use decorator override_settings

from django.test import TestCase, override_settingsclass MyTestCase(TestCase):    @override_settings(CACHES=...)    def test_something(self):        ....


Yes, it is possible to override a setting. From Django documentation: Testing:

For testing purposes it’s often useful to change a setting temporarily and revert to the original value after running the testing code. For this use case Django provides a standard Python context manager ... settings(), which can be used like this:

from django.test import TestCaseclass LoginTestCase(TestCase):    def test_login(self):        # Override the LOGIN_URL setting        with self.settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}):            response = self.client.get(...)

I have tested the above approach with several other settings myself, but not with the particular cache setting, but this is the general idea.

EDIT (credits @Alasdair):

Regrading the particular setting override, the following warning can be found in the documentation:

Altering the CACHESsetting is possible, but a bit tricky if you are using internals that make using of caching, like django.contrib.sessions. For example, you will have to reinitialize the session backend in a test that uses cached sessions and overrides CACHES.