Add assertNumQueries() to testing framework
|Reported by:||Adrian Holovaty||Owned by:||Alex Gaynor|
|Cc:||egmanoj@…, Alexander Koshelev, Mikhail Korobov||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
The Django testing framework should provide
assertNumQueries(), which would assert that a given action executed a given number of database queries. This should work at the view level ("view X uses 3 queries total") or the individual statement level ("model method X uses only 1 query").
This might have to be implemented as two methods -- a "start counting" method and a "stop counting" method:
def test_something(self): self.startCountingQueries() do_something_that_should_only_use_two_queries() self.assertNumQueries(2)
In this example,
startCountingQueries() would reset the counter to 0, and every query would be tallied from that point on.
assertNumQueries() would simply assert the query count was the given number.
Note that this depends on #5415, which provides signals for every SQL query.
Change History (26)
comment:16 Changed 8 years ago by
|Cc:||Alexander Koshelev added|
|Owner:||changed from Kevin Kubasik to Alexander Koshelev|
|Status:||new → assigned|
comment:20 Changed 6 years ago by
|Owner:||changed from Alexander Koshelev to Alex Gaynor|
|Status:||assigned → new|