diff options
| author | Jannis Leidel <jannis@leidel.info> | 2011-05-06 13:29:24 +0000 |
|---|---|---|
| committer | Jannis Leidel <jannis@leidel.info> | 2011-05-06 13:29:24 +0000 |
| commit | 0dc6420b3ebddd7bc2a0059b225d54ac19d4d901 (patch) | |
| tree | 4502a9dc75e486e1197da3fe6b1a99312f5a6bd3 /django | |
| parent | 21027a05c2b82045c376d5fef1c9f5502238e684 (diff) | |
Added TestCase.settings context manager to easily override settings in test methods.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16165 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/test/testcases.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py index 8ce3a7af0f..d85bc27218 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -2,11 +2,12 @@ from __future__ import with_statement import re import sys +from contextlib import contextmanager from functools import wraps from urlparse import urlsplit, urlunsplit from xml.dom.minidom import parseString, Node -from django.conf import settings +from django.conf import settings, UserSettingsHolder from django.core import mail from django.core.management import call_command from django.core.signals import request_started @@ -341,6 +342,22 @@ class TransactionTestCase(ut2.TestCase): """ restore_warnings_state(self._warnings_state) + @contextmanager + def settings(self, **options): + """ + A context manager that temporarily sets a setting and reverts + back to the original value when exiting the context. + """ + old_wrapped = settings._wrapped + override = UserSettingsHolder(settings._wrapped) + try: + for key, new_value in options.items(): + setattr(override, key, new_value) + settings._wrapped = override + yield + finally: + settings._wrapped = old_wrapped + def assertRedirects(self, response, expected_url, status_code=302, target_status_code=200, host=None, msg_prefix=''): """Asserts that a response redirected to a specific URL, and that the |
