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. Decorator and context manager to override settings by jezdez 8 months, 4 weeks ago
  2. TestCase base class to easily temporarily change module values by SmileyChris 1 year, 6 months ago
  3. Deep package test runner by eternicode 11 months, 1 week ago
  4. Breaking tests.py into multiple files by gsakkis 1 year, 10 months ago
  5. Switch template tag by adurdin 3 years, 6 months ago

Comments

(Forgotten your password?)