Capture stack trace when queries are executed
Asserting query counts in Django tests with CaptureQueriesContext
is a great way to maintain/optimize code, but it doesn't provide enough context to find what changed when tests fail during a refactor. I found it helpful to capture some lines from the stack trace when each query is being executed and have them accessible during test runs that use CaptureQueriesContext
to be logged along with the query, when count assertions fail.