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 TestCase
class 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 CACHES
setting 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
.