[Django] #36327: Improve TestCase.assertNumQueries() to allow for multiple databases

14 views
Skip to first unread message

Django

unread,
Apr 14, 2025, 12:08:14 PM4/14/25
to django-...@googlegroups.com
#36327: Improve TestCase.assertNumQueries() to allow for multiple databases
-------------------------------------+-------------------------------------
Reporter: Javier Buzzi | Type: New
| feature
Status: new | Component: Testing
| framework
Version: dev | Severity: Normal
Keywords: query count | Triage Stage:
testcase assertNumQueries | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Relevant discussions:
* https://forum.djangoproject.com/t/add-ability-to-capture-all-the-db-
queries-at-once-during-tests/39650
* https://github.com/pytest-dev/pytest-django/pull/1177

The goal is to enhance the behavior of `TestCase.assertNumQueries()`. In
large Django projects, the data structure is often split into multiple
databases—for example, one for user data, another for business data, one
for events, and even a dedicated one for certain monetary compliance logs.
As a result, tests may need to validate query counts across several
databases. For instance, you might have code that looks like this:

{{{
class TestThing(TestCase):
databases = {"db1", "db2", "db3", "db4"}

def test_thing(self):
with self.assertNumQueries(4, using="db1"),
self.assertNumQueries(0, using="db2"), self.assertNumQueries(1,
using="db3"), self.assertNumQueries(0, using="db4"):
thing()
}}}

The desired improvement is to simplify this syntax. Instead of writing a
separate context manager for each database, you could pass multiple
databases at once. For example:

{{{
class TestThing(TestCase):
databases = {"db1", "db2", "db3", "db4"}

def test_thing(self):
with self.assertNumQueries(5, using={"db1", "db2", "db3", "db4"}):
thing()
}}}

Alternatively, you might be able to use a special keyword like `__all__`
to achieve the same effect:

{{{
with self.assertNumQueries(5, using="__all__"):
...
}}}

This approach not only makes the test code more manageable but also helps
improve clarity by consolidating query count assertions across all
databases.
--
Ticket URL: <https://code.djangoproject.com/ticket/36327>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 14, 2025, 12:13:06 PM4/14/25
to django-...@googlegroups.com
#36327: Improve TestCase.assertNumQueries() to allow for multiple databases
-------------------------------------+-------------------------------------
Reporter: Javier Buzzi | Owner: (none)
Type: New feature | Status: new
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: query count | Triage Stage:
testcase assertNumQueries | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Javier Buzzi):

* has_patch: 0 => 1


Old description:
New description:

[https://github.com/django/django/pull/19380 PR 19380]
--
Ticket URL: <https://code.djangoproject.com/ticket/36327#comment:1>

Django

unread,
Apr 14, 2025, 12:18:25 PM4/14/25
to django-...@googlegroups.com
#36327: Improve TestCase.assertNumQueries() to allow for multiple databases
-------------------------------------+-------------------------------------
Reporter: Javier Buzzi | Owner: Javier
| Buzzi
Type: New feature | Status: assigned
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: query count | Triage Stage:
testcase assertNumQueries | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Javier Buzzi):

* owner: (none) => Javier Buzzi
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36327#comment:2>

Django

unread,
Apr 14, 2025, 2:51:51 PM4/14/25
to django-...@googlegroups.com
#36327: Improve TestCase.assertNumQueries() to allow for multiple databases
-------------------------------------+-------------------------------------
Reporter: Javier Buzzi | Owner: Javier
| Buzzi
Type: New feature | Status: closed
Component: Testing framework | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: query count | Triage Stage:
testcase assertNumQueries | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* resolution: => wontfix
* status: assigned => closed

Comment:

As explained in the forum post, I'll close this as `wontfix` since a
viable approach using `subTest` and `assertNumQueries` has been proposed
in the related discussion, which covers the use case without requiring a
framework change (to my best understanding).
--
Ticket URL: <https://code.djangoproject.com/ticket/36327#comment:3>

Django

unread,
Apr 14, 2025, 3:41:28 PM4/14/25
to django-...@googlegroups.com
#36327: Improve TestCase.assertNumQueries() to allow for multiple databases
-------------------------------------+-------------------------------------
Reporter: Javier Buzzi | Owner: Javier
| Buzzi
Type: New feature | Status: closed
Component: Testing framework | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: query count | Triage Stage:
testcase assertNumQueries | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak):

I'm also skeptic. I'd prefer to explicitly assert the number of queries
per database.
--
Ticket URL: <https://code.djangoproject.com/ticket/36327#comment:4>
Reply all
Reply to author
Forward
0 new messages