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. Db Mock by akhavr 5 years, 9 months ago
  2. Run a testcase with custom INSTALLED_APPS by vdboor 6 months, 1 week ago
  3. TestCase base class to easily temporarily change module values by SmileyChris 2 years, 10 months ago
  4. Decorator and context manager to override settings by jezdez 2 years ago
  5. Reset cache between tests by peterbe 3 years, 6 months ago

Comments

(Forgotten your password?)