Last Updated: February 25, 2016
· prudnikov

Custom settings when testing Django app

Sometimes you want to use different settings in your unittest test cases. Sometimes I see people doing it wrong, but there is an easy way of doing this by using override_settings decorator.

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

class MyTest(TestCase):
    def test_public_access(self):
        self.assertEqual(settings.INTERNAL_IPS, ())

    def test_internal_access(self):
        self.assertEqual(settings.INTERNAL_IPS, ('',))

    def test_custom_settings(self):
            getattr(settings, "SOME_NEW_SETTINGS", "NO"),