154

Is there any simple mechanism for overriding Django settings for a unit test? I have a manager on one of my models that returns a specific number of the latest objects. The number of objects it returns is defined by a NUM_LATEST setting.

This has the potential to make my tests fail if someone were to change the setting. How can I override the settings on setUp() and subsequently restore them on tearDown()? If that isn't possible, is there some way I can monkey patch the method or mock the settings?

EDIT: Here is my manager code:

class LatestManager(models.Manager):
    """
    Returns a specific number of the most recent public Articles as defined by 
    the NEWS_LATEST_MAX setting.
    """
    def get_query_set(self):
        num_latest = getattr(settings, 'NEWS_NUM_LATEST', 10)
        return super(LatestManager, self).get_query_set().filter(is_public=True)[:num_latest]

The manager uses settings.NEWS_LATEST_MAX to slice the queryset. The getattr() is simply used to provide a default should the setting not exist.

1
  • It changed in the meantime; the former accepted one was this one ;)
    – Anto
    Jan 14, 2015 at 1:28

16 Answers 16

202

EDIT: This answer applies if you want to change settings for a small number of specific tests.

Since Django 1.4, there are ways to override settings during tests: https://docs.djangoproject.com/en/stable/topics/testing/tools/#overriding-settings

TestCase will have a self.settings context manager, and there will also be an @override_settings decorator that can be applied to either a test method or a whole TestCase subclass.

These features did not exist yet in Django 1.3.

If you want to change settings for all your tests, you'll want to create a separate settings file for test, which can load and override settings from your main settings file. There are several good approaches to this in the other answers; I have seen successful variations on both hspander's and dmitrii's approaches.

4
  • 4
    I'd say this is the best way of doing this now in Django 1.4+ Jun 28, 2013 at 12:59
  • How do you later access that setting from within the tests? Best I've found is something like self.settings().wrapped.MEDIA_ROOT, but that's pretty terrible.
    – mlissner
    Oct 22, 2014 at 17:56
  • 2
    Newer versions of Django have a specific context manager for this: docs.djangoproject.com/en/1.8/topics/testing/tools/…
    – Akhorus
    Nov 6, 2015 at 14:26
  • 2
    My favorite: @modify_settings(MIDDLEWARE_CLASSES=... (thank you for this answer)
    – guettli
    Oct 14, 2016 at 9:28
55

You can do anything you like to the UnitTest subclass, including setting and reading instance properties:

from django.conf import settings

class MyTest(unittest.TestCase):
   def setUp(self):
       self.old_setting = settings.NUM_LATEST
       settings.NUM_LATEST = 5 # value tested against in the TestCase

   def tearDown(self):
       settings.NUM_LATEST = self.old_setting

Since the django test cases run single-threaded, however, I'm curious about what else may be modifying the NUM_LATEST value? If that "something else" is triggered by your test routine, then I'm not sure any amount of monkey patching will save the test without invalidating the veracity of the tests itself.

5
  • Your example worked. This has been an eye-opener in terms of the scope of unit testing and how the settings in the tests file propagate down through the call stack.
    – Soviut
    May 27, 2009 at 3:48
  • This does not work with settings.TEMPLATE_LOADERS... So this is not general way at least, the settings or Django is not reloaded or anything with this trick.
    – Ciantic
    Jul 17, 2010 at 19:41
  • 1
    this is good example for version Django older then 1.4. For >= 1.4 answer stackoverflow.com/a/6415129/190127 more correct
    – Oduvan
    Jan 9, 2013 at 14:38
  • Use docs.djangoproject.com/en/dev/topics/testing/tools/… Patching with setUp and tearDown like this is a great way to make really fragile tests that are more verbose than they need to be. If you need to patch something like this use something like flexmock. May 29, 2014 at 17:34
  • "Since the django test cases run single-threaded": which is no longer the case in Django 1.9.
    – Wtower
    Dec 3, 2015 at 10:29
34

You can pass --settings option when running tests

python manage.py test --settings=mysite.settings_local
2
  • it stopped to find apps which are located in settings.dev which is extension of settings.base
    – holms
    Feb 26, 2018 at 13:02
  • 8
    I think it will be dangerous if someone forget to add the settings parameters once.
    – ramwin
    Jun 12, 2018 at 3:51
26

Although overriding settings configuration on runtime might help, in my opinion you should create a separate file for testing. This saves lot of configuration for testing and this would ensure that you never end up doing something irreversible (like cleaning staging database).

Say your testing file exists in 'my_project/test_settings.py', add

settings = 'my_project.test_settings' if 'test' in sys.argv else 'my_project.settings'

in your manage.py. This will ensure that when you run python manage.py test you use test_settings only. If you are using some other testing client like pytest, you could as easily add this to pytest.ini

2
  • 2
    I think this is a good solution for me. I have many too many tests and code which is using cache. It will be difficult for me to override the settings one by one. I will create two config file and determine which one to use. The MicroPyramid's answer is also avaiable, but it will be dangerous if I forgot to add the settings parameters once.
    – ramwin
    Jun 12, 2018 at 3:50
  • For me, this also caused testing settings to be used when running the server with runserver. I had to use sys.argv[1:2] == ["test"] instead of 'test' in sys.argv in the condition to make this work as required (reference).
    – Clerni
    Jul 5, 2023 at 17:43
21

You can override setting even for a single test function.

from django.test import TestCase, override_settings

class SomeTestCase(TestCase):

    @override_settings(SOME_SETTING="some_value")
    def test_some_function():
        

or you can override setting for each function in class.

@override_settings(SOME_SETTING="some_value")
class SomeTestCase(TestCase):

    def test_some_function():
        
20

Update: the solution below is only needed on Django 1.3.x and earlier. For >1.4 see slinkp's answer.

If you change settings frequently in your tests and use Python ≥2.5, this is also handy:

from contextlib import contextmanager

class SettingDoesNotExist:
    pass

@contextmanager
def patch_settings(**kwargs):
    from django.conf import settings
    old_settings = []
    for key, new_value in kwargs.items():
        old_value = getattr(settings, key, SettingDoesNotExist)
        old_settings.append((key, old_value))
        setattr(settings, key, new_value)
    yield
    for key, old_value in old_settings:
        if old_value is SettingDoesNotExist:
            delattr(settings, key)
        else:
            setattr(settings, key, old_value)

Then you can do:

with patch_settings(MY_SETTING='my value', OTHER_SETTING='other value'):
    do_my_tests()
3
  • This is really cool solution. For some reason my settings were not working properly in the unit tests. Very elegant solution, thanks for sharing.
    – Tomas
    Mar 22, 2012 at 21:26
  • I am using this code, but I had problems with cascading test failures, because settings would not get reverted if the test in question failed. To address this, I added a try/finally around the yield statement, with the final part of the function contained in the finally block, so that settings are always reverted. May 29, 2012 at 17:36
  • I'll edit the answer for posterity. I hope I'm doing this right! :) May 29, 2012 at 17:37
15

For pytest users.

The biggest issue is:

  • override_settings doesn't work with pytest.
  • Subclassing Django's TestCase will make it work but then you can't use pytest fixtures.

The solution is to use the settings fixture documented here.

Example

def test_with_specific_settings(settings):
    settings.DEBUG = False
    settings.MIDDLEWARE = []
    ..

And in case you need to update multiple fields

def override_settings(settings, kwargs):
    for k, v in kwargs.items():
        setattr(settings, k, v)


new_settings = dict(
    DEBUG=True,
    INSTALLED_APPS=[],
)


def test_with_specific_settings(settings):
    override_settings(settings, new_settings)
1
  • 1
    unless I've done something wrong, I can use the override_settings decorator with pytest; my test classes do not subclass TestCase
    – A G
    Aug 8, 2021 at 15:30
13

@override_settings is great if you don't have many differences between your production and testing environment configurations.

In other case you'd better just have different settings files. In this case your project will look like this:

your_project
    your_app
        ...
    settings
        __init__.py
        base.py
        dev.py
        test.py
        production.py
    manage.py

So you need to have your most of your settings in base.py and then in other files you need to import all everything from there, and override some options. Here's what your test.py file will look like:

from .base import *

DEBUG = False

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'app_db_test'
    }
}

PASSWORD_HASHERS = (
    'django.contrib.auth.hashers.MD5PasswordHasher',
)

LOGGING = {}

And then you either need to specify --settings option as in @MicroPyramid answer, or specify DJANGO_SETTINGS_MODULE environment variable and then you can run your tests:

export DJANGO_SETTINGS_MODULE=settings.test
python manage.py test 
2
  • Hello . Dmitrii, thanks for your answer am having the same case with this answer, but i would like to get more guidance on how the app will know, the environment we are in (testing or production), have a look on my branch, check out my repo github.com/andela/ah-backend-iroquois/tree/develop/authors , like how will i handle that logic ? Aug 30, 2018 at 18:26
  • Because i use nosetests to run tests , now how will this be run?, in the testing environment not in development environment Aug 30, 2018 at 18:28
7

I created a new settings_test.py file which would import everything from settings.py file and modify whatever is different for testing purpose. In my case I wanted to use a different cloud storage bucket when testing. enter image description here

settings_test.py:

from project1.settings import *
import os

CLOUD_STORAGE_BUCKET = 'bucket_name_for_testing'

manage.py:

def main():

    # use seperate settings.py for tests
    if 'test' in sys.argv:
        print('using settings_test.py')
        os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project1.settings_test')
    else:
        os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project1.settings')

    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)
3
  • This may work. But if we try to import some constant from settings file in our code - from django.conf import settings we will still get value from old settings.py file. we wont get new value from settings_test.py file
    – Aseem
    Jan 11, 2023 at 7:15
  • For me, this also caused testing settings to be used when running the server with runserver. I had to use sys.argv[1:2] == ["test"] instead of 'test' in sys.argv in the condition to make this work as required (reference).
    – Clerni
    Jul 5, 2023 at 17:32
  • good job, bro. Manage.py tests works as intended. Keep it up.
    – Koops
    Jul 25, 2023 at 17:24
3

Found this while trying to fix some doctests... For completeness I want to mention that if you're going to modify the settings when using doctests, you should do it before importing anything else...

>>> from django.conf import settings

>>> settings.SOME_SETTING = 20

>>> # Your other imports
>>> from django.core.paginator import Paginator
>>> # etc
0
2

I'm using pytest.

I managed to solve this the following way:

import django    
import app.setting
import modules.that.use.setting

# do some stuff with default setting
setting.VALUE = "some value"
django.setup()
import importlib
importlib.reload(app.settings)
importlib.reload(modules.that.use.setting)
# do some stuff with settings new value
1
  • 1
    An easier way for pytest users is to use the settings fixture. See this answer from Pithikos for an example. Jul 26, 2022 at 14:57
2

You can override settings in test in this way:

from django.test import TestCase, override_settings

test_settings = override_settings(
    DEFAULT_FILE_STORAGE='django.core.files.storage.FileSystemStorage',
    PASSWORD_HASHERS=(
        'django.contrib.auth.hashers.UnsaltedMD5PasswordHasher',
    )
)


@test_settings
class SomeTestCase(TestCase):
    """Your test cases in this class"""

And if you need these same settings in another file you can just directly import test_settings.

0

If you have multiple test files placed in a subdirectory (python package), you can override settings for all these files based on condition of presence of 'test' string in sys.argv

app
  tests
    __init__.py
    test_forms.py
    test_models.py

__init__.py:

import sys
from project import settings

if 'test' in sys.argv:
    NEW_SETTINGS = {
        'setting_name': value,
        'another_setting_name': another_value
    }
    settings.__dict__.update(NEW_SETTINGS)

Not the best approach. Used it to change Celery broker from Redis to Memory.

0

One setting for all tests in a testCase

class TestSomthing(TestCase):
    def setUp(self, **kwargs):
        with self.settings(SETTING_BAR={ALLOW_FOO=True})
            yield

override one setting in the testCase

from django.test import override_settings

    @override_settings(SETTING_BAR={ALLOW_FOO=False})
    def i_need_other_setting(self):
        ...

Important


Even though you are overriding these settings this will not apply to settings that your server initialize stuff with because it is already initialized, to do that you will need to start django with another setting module.

0

I really like using a custom test runner (taken from @meshy's answer over here), since:

  • It doesn't rely on command-line arguments (which could be forgotten).
  • It doesn't require modifying your actual test cases.

Put the following in myapp/test_runner.py:

from django.conf import settings
from django.test.runner import DiscoverRunner

class MyTestRunner(DiscoverRunner):
    def setup_test_environment(self):
        super().setup_test_environment()
        settings.NEWS_NUM_LATEST = 42
        # ...any other settings you want to override...

and the following in project/settings.py:

TEST_RUNNER = 'myapp.test_runner.MyTestRunner'
0

If you don't want to use the override_settings decorator you can use this syntax

with override_settings(CUSTOMER_ORDERS_ALLOWED=0):
    # Test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.