change settings locally in an individual test

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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)

More like this

  1. TestCase base class to easily temporarily change module values by SmileyChris 3 years, 9 months ago
  2. TestSettingsManager: temporarily change settings for tests by carljm 5 years, 7 months ago
  3. Run a testcase with custom INSTALLED_APPS by vdboor 1 year, 5 months ago
  4. Add a print link to the admin to print individual record by jimmylam 5 years, 5 months ago
  5. Decorator and context manager to override settings by jezdez 2 years, 11 months ago

Comments

(Forgotten your password?)