summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2010-10-12 03:33:19 +0000
committerAlex Gaynor <alex.gaynor@gmail.com>2010-10-12 03:33:19 +0000
commit5506653b777d7547d21ea2d74e9588fb94314b77 (patch)
tree34b6e6c3de02848bf793bd3088681ae29c44238d /docs
parentceef628c192127d5e231613bc280d68dc0927fa3 (diff)
Fixed #5416 -- Added TestCase.assertNumQueries, which tests that a given function executes the correct number of queries.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14183 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'docs')
-rw-r--r--docs/topics/testing.txt26
1 files changed, 26 insertions, 0 deletions
diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
index 81cc4809d5..465807021a 100644
--- a/docs/topics/testing.txt
+++ b/docs/topics/testing.txt
@@ -1372,6 +1372,32 @@ cause of an failure in your test suite.
implicit ordering, you will need to apply a ``order_by()`` clause to your
queryset to ensure that the test will pass reliably.
+.. method:: TestCase.assertNumQueries(num, func, *args, **kwargs):
+
+ .. versionadded:: 1.3
+
+ Asserts that when ``func`` is called with ``*args`` and ``**kwargs`` that
+ ``num`` database queries are executed.
+
+ If a ``"using"`` key is present in ``kwargs`` it is used as the database
+ alias for which to check the number of queries. If you wish to call a
+ function with a ``using`` parameter you can do it by wrapping the call with
+ a ``lambda`` to add an extra parameter::
+
+ self.assertNumQueries(7, lambda: my_function(using=7))
+
+ If you're using Python 2.5 or greater you can also use this as a context
+ manager::
+
+ # 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.assertNumQueries(2):
+ Person.objects.create(name="Aaron")
+ Person.objects.create(name="Daniel")
+
+
.. _topics-testing-email:
E-mail services