diff --git a/docs/helpers.rst b/docs/helpers.rst index d39acd93f..5e8cb176a 100644 --- a/docs/helpers.rst +++ b/docs/helpers.rst @@ -278,22 +278,23 @@ Example assert settings.USE_TZ +.. fixture:: django_assert_num_queries + ``django_assert_num_queries`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. fixture:: django_assert_num_queries +.. py:function:: django_assert_num_queries(connection=None, info=None) + + :param connection: optional non-default DB connection + :param str info: optional info message to display on failure This fixture allows to check for an expected number of DB queries. -It wraps `django.test.utils.CaptureQueriesContext`. A non-default DB -connection can be passed in using the `connection` keyword argument, and it -will yield the wrapped CaptureQueriesContext instance. +It wraps `django.test.utils.CaptureQueriesContext` and yields the wrapped +CaptureQueriesContext instance. -Example -""""""" - -:: +Example usage:: def test_queries(django_assert_num_queries): with django_assert_num_queries(3) as captured: @@ -304,20 +305,21 @@ Example assert 'foo' in captured.captured_queries[0]['sql'] +.. fixture:: django_assert_max_num_queries + ``django_assert_max_num_queries`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. fixture:: django_assert_max_num_queries +.. py:function:: django_assert_num_queries(connection=None, info=None) + + :param connection: optional non-default DB connection + :param str info: optional info message to display on failure This fixture allows to check for an expected maximum number of DB queries. It is a specialized version of :fixture:`django_assert_num_queries`. - -Example -""""""" - -:: +Example usage:: def test_max_queries(django_assert_max_num_queries): with django_assert_max_num_queries(3): diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index 2fd32ed11..a04326e40 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -407,7 +407,7 @@ def _live_server_helper(request): @contextmanager -def _assert_num_queries(config, num, exact=True, connection=None): +def _assert_num_queries(config, num, exact=True, connection=None, info=None): from django.test.utils import CaptureQueriesContext if connection is None: @@ -429,6 +429,8 @@ def _assert_num_queries(config, num, exact=True, connection=None): num_performed == 1 and "1 was" or "%d were" % (num_performed,) ), ) + if info: + msg += "\n{}".format(info) if verbose: sqls = (q["sql"] for q in context.captured_queries) msg += "\n\nQueries:\n========\n\n%s" % "\n\n".join(sqls) diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index f5bd706bc..2693ef296 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -158,6 +158,32 @@ def test_django_assert_num_queries_db_connection(django_assert_num_queries): pass +@pytest.mark.django_db +def test_django_assert_num_queries_output_info(django_testdir): + django_testdir.create_test_module(""" + from django.contrib.contenttypes.models import ContentType + import pytest + + @pytest.mark.django_db + def test_queries(django_assert_num_queries): + with django_assert_num_queries( + num=2, + info="Expected: 1 for select all, 1 for count" + ): + list(ContentType.objects.all()) + ContentType.objects.count() + ContentType.objects.first() # additional wrong query + """) + result = django_testdir.runpytest_subprocess('--tb=short', '-v') + result.stdout.fnmatch_lines([ + '*Expected to perform 2 queries but 3 were done*', + '*Expected: 1 for select all, 1 for count*', + '*Queries:*', + '*========*', + ]) + assert result.ret == 1 + + class TestSettings: """Tests for the settings fixture, order matters"""