From 0dc6420b3ebddd7bc2a0059b225d54ac19d4d901 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 6 May 2011 13:29:24 +0000 Subject: 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 --- docs/topics/testing.txt | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'docs') diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt index fbc6a7afe8..91d37b1bf1 100644 --- a/docs/topics/testing.txt +++ b/docs/topics/testing.txt @@ -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 ~~~~~~~~~~~~~~~~~~~~~~~~ -- cgit v1.3