Date: 2021-11-18
In a unit test, you can use Django’s CaptureQueriesContext to capture the queries that were made by the code covered by the context manager. This allows you to assert the number of queries executed as part of your test:
from django.db import connection
from django.test.utils import CaptureQueriesContext
with CaptureQueriesContext(connection) as ctx:
some_function_that_executes_qeuries()
# assert that 4 queries were executed
assert len(ctx.captured_queries) == 4