Override test settings from a file

Question:

For context, when one wants to change settings in a test (yes, it’s ok to change it there), we can use override_settings() or modify_settings() (as observed here). That works when running the tests in parallel too.

Generally speaking, I do something like this

from django.test import TestCase, override_settings

@override_settings(LOGIN_URL='/other/login/')
class LoginTestCase(TestCase):

    def test_login(self):
        response = self.client.get('/sekrit/')
        self.assertRedirects(response, '/other/login/?next=/sekrit/')

Thing is, the list of settings can get big. So would to get the key-values from another file.

How to override from a file instead of having a long list like LOGIN_URL='/other/login/', ...?

Answers:

Maybe it’s not file config, but you can use ** to pass dict as kwargs to decorator:

from .TEST_CONF import EXAMPLE_CONF

@override_settings(**EXAMPLE_CONF)
class TestExample(TestCase):
    def test_sth(self):
        from django.conf import settings
        self.fail('{} {} {}'.format(settings.A, settings.B, settings.CELERY_ALWAYS_EAGER))

and TEST_CONF file looks like this:

EXAMPLE_CONF = {
    'CELERY_ALWAYS_EAGER': True,
    'A': 'sth',
    'B': 'else',
}

result of this fail is:

AssertionError: sth else True
Answered By: PTomasz