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 /docs | |
| 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 'docs')
| -rw-r--r-- | docs/topics/testing.txt | 25 |
1 files changed, 25 insertions, 0 deletions
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 ~~~~~~~~~~~~~~~~~~~~~~~~ |
