test.TransactionTestCase.assertNumQueries()

TransactionTestCase.assertNumQueries(num, func, *args, **kwargs) [source]

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))

You can also use this as a context manager:

with self.assertNumQueries(2):
    Person.objects.create(name="Aaron")
    Person.objects.create(name="Daniel")
doc_Django
2016-10-09 18:40:09
Comments
Leave a Comment

Please login to continue.