Skip to main content

Provide a way to override Django's settings when running tests

Project description

django-override-settings provides an easy way to override settings in Django tests.

The override_settings class can be used as either a class or method decorator or as a context manager to temporarily override the values of settings. After each test case has finished (when using it as a decorator) or after the context manager has exited, it resets the values in django.conf.settings to what they were before. This prevents side-effects from creeping in and lets each test case run in its own sandbox.

This package also provides two convenience functions (with_apps and without_apps) to modify just INSTALLED_APPS as well as a special object (SETTING_DELETED) to run tests without a given setting defined.

The functionality in this package will eventually be superseded when Django 1.4 is released as it will come with a built-in override_settings. But for those maintaining pre-1.4 codebases, hopefully this package comes in handy.


We’re on PyPI:

pip install django-override-settings


If you have a bunch of tests that require a given setting, you can decorate the class and each test case will use that value. For example:

from django.conf import settings
from django.test import TestCase
from override_settings import override_settings

class TestFoo(TestCase):
    def test_foo(self):
        self.assertEqual(settings.FOO, "abc")

Or you can decorate a single test case and have it only apply on that method:

class TestBar(TestCase):

    def test_bar(self):
        self.assertEqual(settings.BAR, "abc")

    def test_bar_no_decoration(self):
        self.assertEqual(settings.BAR, "123")

You can also use it as a context manager:

class TestBar(TestCase):
    def test_bar(self):
        self.assertEqual(settings.BAR, "123")

        with override_settings(BAR="abc")
            self.assertEqual(settings.BAR, "abc")

        self.assertEqual(settings.BAR, "123")

To modify just INSTALLED_APPS, use with_apps or without_apps:

from override_settings import with_apps, without_apps

class TestAppModifiers(TestCase):
    def test_humanize(self):
        # ...

    def test_no_sites(self):
        # ...

To run tests without a setting, use SETTING_DELETED:

from override_settings import override_settings, SETTING_DELETED

class TestMissingSetting(TestCase):
    def test_delete_custom_option(self):
        Useful to make sure a missing setting raises an Exception.
        self.assertRaises(AttributeError, getattr, settings, 'CUSTOM_OPTION')


  • Django >= 1.2



If you notice any bugs, please open a ticket.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-override-settings, version 1.2
Filename, size File type Python version Upload date Hashes
Filename, size django-override-settings-1.2.tar.gz (4.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page