Django: is there a way to count SQL queries from an unit test? Django: is there a way to count SQL queries from an unit test? django django

Django: is there a way to count SQL queries from an unit test?


Since Django 1.3 there is a assertNumQueries available exactly for this purpose.

One way to use it (as of Django 3.2) is as a context manager:

# measure queries of some_func and some_func2with self.assertNumQueries(2):    result = some_func()    result2 = some_func2()


Vinay's response is correct, with one minor addition.

Django's unit test framework actually sets DEBUG to False when it runs, so no matter what you have in settings.py, you will not have anything populated in connection.queries in your unit test unless you re-enable debug mode. The Django docs explain the rationale for this as:

Regardless of the value of the DEBUG setting in your configuration file, all Django tests run with DEBUG=False. This is to ensure that the observed output of your code matches what will be seen in a production setting.

If you're certain that enabling debug will not affect your tests (such as if you're specifically testing DB hits, as it sounds like you are), the solution is to temporarily re-enable debug in your unit test, then set it back afterward:

def test_myself(self):    from django.conf import settings    from django.db import connection    settings.DEBUG = True    connection.queries = []    # Test code as normal    self.assert_(connection.queries)    settings.DEBUG = False


If you are using pytest, pytest-django has django_assert_num_queries fixture for this purpose:

def test_queries(django_assert_num_queries):    with django_assert_num_queries(3):        Item.objects.create('foo')        Item.objects.create('bar')        Item.objects.create('baz')