Skip to content

Commit d23cf6e

Browse files
MrtnBckrblueyed
authored andcommitted
Add parameter info to fixture assert_num_queries to display additional message on failure.
1 parent 9804e61 commit d23cf6e

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

docs/helpers.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ Example
286286
This fixture allows to check for an expected number of DB queries.
287287

288288
It wraps `django.test.utils.CaptureQueriesContext`. A non-default DB
289-
connection can be passed in using the `connection` keyword argument, and it
290-
will yield the wrapped CaptureQueriesContext instance.
289+
connection can be passed in using the `connection` keyword argument, an
290+
additional info message which is displayed on fail can be passed in using
291+
the `info` keyword argument, and it will yield the wrapped
292+
CaptureQueriesContext instance.
291293

292294

293295
Example

pytest_django/fixtures.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def _live_server_helper(request):
407407

408408

409409
@contextmanager
410-
def _assert_num_queries(config, num, exact=True, connection=None):
410+
def _assert_num_queries(config, num, exact=True, connection=None, info=None):
411411
from django.test.utils import CaptureQueriesContext
412412

413413
if connection is None:
@@ -426,6 +426,8 @@ def _assert_num_queries(config, num, exact=True, connection=None):
426426
num_queries == 1 and "1 was" or "%d were" % (num_queries,)
427427
),
428428
)
429+
if info:
430+
msg += "\n{}".format(info)
429431
if verbose:
430432
sqls = (q["sql"] for q in context.captured_queries)
431433
msg += "\n\nQueries:\n========\n\n%s" % "\n\n".join(sqls)

tests/test_fixtures.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,32 @@ def test_django_assert_num_queries_db_connection(django_assert_num_queries):
158158
pass
159159

160160

161+
@pytest.mark.django_db
162+
def test_django_assert_num_queries_output_info(django_testdir):
163+
django_testdir.create_test_module("""
164+
from django.contrib.contenttypes.models import ContentType
165+
import pytest
166+
167+
@pytest.mark.django_db
168+
def test_queries(django_assert_num_queries):
169+
with django_assert_num_queries(
170+
num=2,
171+
info="Expected: 1 for select all, 1 for count"
172+
):
173+
list(ContentType.objects.all())
174+
ContentType.objects.count()
175+
ContentType.objects.first() # additional wrong query
176+
""")
177+
result = django_testdir.runpytest_subprocess('--tb=short', '-v')
178+
result.stdout.fnmatch_lines([
179+
'*Expected to perform 2 queries but 3 were done*',
180+
'*Expected: 1 for select all, 1 for count*',
181+
'*Queries:*',
182+
'*========*',
183+
])
184+
assert result.ret == 1
185+
186+
161187
class TestSettings:
162188
"""Tests for the settings fixture, order matters"""
163189

0 commit comments

Comments
 (0)