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
This commit is contained in:
Jannis Leidel 2011-05-06 13:29:24 +00:00
parent 21027a05c2
commit 0dc6420b3e
3 changed files with 70 additions and 4 deletions

View file

@ -1351,6 +1351,31 @@ For example::
This test case will flush *all* the test databases before running
``testIndexPageView``.
Overriding settings
~~~~~~~~~~~~~~~~~~~
.. method:: TestCase.settings
.. versionadded:: 1.4
For testing purposes it's often useful to change a setting temporarily
and revert to the original value after running the testing code. For
this use case Django provides a standard `Python context manager`_
:meth:`~django.test.TestCase.settings`, which can be used like this::
from django.test import TestCase
class LoginTestCase(TestCase):
def test_overriding_settings(self):
with self.settings(LOGIN_URL='/other/login/'):
response = self.client.get('/sekrit/')
self.assertRedirects(response, '/other/login/?next=/sekrit/')
This example will override the :setting:`LOGIN_URL` setting for the code
in the ``with`` block and reset its value to the previous state afterwards.
.. _`Python context manager`: http://www.python.org/dev/peps/pep-0343/
Emptying the test outbox
~~~~~~~~~~~~~~~~~~~~~~~~