#21462 closed New feature (fixed)

Making assertNumQueries print the list of queries executed on failure

Reported by: dominicrodger Owned by: nobody
Component: Testing framework Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, when assertNumQueries fails, the output is perhaps less helpful than it might be:

Traceback (most recent call last):
  File "/home/dom/.virtualenvs/kanisa/src/kanisa/kanisa/tests/views/public.py", line 31, in test_kanisa_root_view
    [banner1.pk, banner2.pk, banner3.pk, banner5.pk, ])
  File "/home/dom/.virtualenvs/kanisa/src/kanisa/.tox/py27-1.5.X/local/lib/python2.7/site-packages/django/test/testcases.py", line 195, in __exit__
    executed, self.num
AssertionError: 5 queries executed, 99 expected

When an assertNumQueries check fails, the first thing I (and I'd guess everyone else too) want to know is what queries ran, so it'd be awesome if an assertNumQueries failure printed the list of queries. Charlie Denton has a blog post describing how he does this (http://meshy.co.uk/posts/debugging-assertnumqueries-tests/), but I'd like to see this included in Django itself - since it seems generally helpful.

This was discussed on django-developers (https://groups.google.com/forum/#!topic/django-developers/8aUL9d9yyzg).

Change History (4)

comment:1 Changed 20 months ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 19 months ago by dominicrodger

Pull request created - https://github.com/django/django/pull/2055.

I'm not sure what kind of testing this needs - I don't want tests to depend on the SQL generated, so for now I've just amended the test for this to check that "Captured queries were" is included in the assertion. That feels a bit limited, so better ideas welcome.

This is my first non-docs contribution to Django, so do let me know if there's anything else that needs doing to get this committed.

comment:3 Changed 19 months ago by charettes

  • Triage Stage changed from Accepted to Ready for checkin

Test is looking good and the full test suite passes on Py2-3 SQlite3. The added test also passes on PostgreSQL (Py2-3) and MySQL.

Will commit with the minor suggested changes applied.

comment:4 Changed 19 months ago by Simon Charette <charette.s@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 5cd6477fd6ea31eeb4d281e8e431b7a5fb8038a1:

Fixed #21462 -- Made assertNumQueries print executed queries on failure.

Note: See TracTickets for help on using tickets.
Back to Top