Add assertNumQueries() to testing framework
|Reported by:||adrian||Owned by:||Alex|
|Cc:||egmanoj@…, alexkoshelev, kmike||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:1 Changed 8 years ago by adrian
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted
Changed 6 years ago by Manoj Govindan <egmanoj@…>
comment:16 Changed 6 years ago by alexkoshelev
- Cc alexkoshelev added
- Owner changed from kkubasik to alexkoshelev
- Status changed from new to assigned
comment:20 Changed 5 years ago by Alex
- Owner changed from alexkoshelev to Alex
- Status changed from assigned to new