From a678e9ea6589bd95d9d30e86a50d3694ebf60908 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Tue, 31 Jan 2012 19:23:09 +0000 Subject: Fixed #17604 - Added context-manager capability to assertTemplateUsed and assertTemplateNotUsed. Thanks Greg Müllegger. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@17412 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- docs/releases/1.4.txt | 15 +++++++++++++++ docs/topics/testing.txt | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) (limited to 'docs') diff --git a/docs/releases/1.4.txt b/docs/releases/1.4.txt index 8a7bfb2011..f2c97f603a 100644 --- a/docs/releases/1.4.txt +++ b/docs/releases/1.4.txt @@ -946,6 +946,21 @@ apply URL escaping again. This is wrong for URLs whose unquoted form contains a ``%xx`` sequence, but such URLs are very unlikely to happen in the wild, since they would confuse browsers too. +``assertTemplateUsed`` and ``assertTemplateNotUsed`` as context manager +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +It is now possible to check whether a template was used or not in a block of +code with the :meth:`~django.test.testcase.TestCase.assertTemplateUsed` and +:meth:`~django.test.testcase.TestCase.assertTemplateNotUsed` assertions. They +can be used as a context manager:: + + with self.assertTemplateUsed('index.html'): + render_to_string('index.html') + with self.assertTemplateNotUsed('base.html'): + render_to_string('index.html') + +See the :ref:`assertion documentation` for more information. + Database connections after running the test suite ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt index ea2c52b097..f0f0b445f9 100644 --- a/docs/topics/testing.txt +++ b/docs/topics/testing.txt @@ -1575,11 +1575,30 @@ your test suite. The name is a string such as ``'admin/index.html'``. + .. versionadded:: 1.4 + + You can also use this as a context manager. The code that is executed + under the with statement is then observed instead of a response:: + + # This is necessary in Python 2.5 to enable the with statement, in 2.6 + # and up it is no longer necessary. + from __future__ import with_statement + + with self.assertTemplateUsed('index.html'): + render_to_string('index.html') + with self.assertTemplateUsed(template_name='index.html'): + render_to_string('index.html') + .. method:: TestCase.assertTemplateNotUsed(response, template_name, msg_prefix='') Asserts that the template with the given name was *not* used in rendering the response. + .. versionadded:: 1.4 + + You can use this as a context manager in the same way as + :func:`~TestCase.assertTemplateUsed`. + .. method:: TestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, msg_prefix='') Asserts that the response return a ``status_code`` redirect status, it -- cgit v1.3