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

This commit is contained in:
Dominic Rodger 2013-12-08 23:07:04 +00:00 committed by Simon Charette
parent 75924cfa6d
commit 5cd6477fd6
3 changed files with 10 additions and 2 deletions

View File

@ -103,8 +103,12 @@ class _AssertNumQueriesContext(CaptureQueriesContext):
return
executed = len(self)
self.test_case.assertEqual(
executed, self.num, "%d queries executed, %d expected" % (
executed, self.num
executed, self.num,
"%d queries executed, %d expected\nCaptured queries were:\n%s" % (
executed, self.num,
'\n'.join(
query['sql'] for query in self.captured_queries
)
)
)

View File

@ -713,6 +713,9 @@ Miscellaneous
``select_related('foo').select_related('bar')``. Previously the latter would
have been equivalent to ``select_related('bar')``.
* :meth:`~django.test.TransactionTestCase.assertNumQueries` now prints
out the list of executed queries if the assertion fails.
Features deprecated in 1.7
==========================

View File

@ -192,6 +192,7 @@ class AssertNumQueriesContextManagerTests(TestCase):
with self.assertNumQueries(2):
Person.objects.count()
self.assertIn("1 queries executed, 2 expected", str(exc_info.exception))
self.assertIn("Captured queries were", str(exc_info.exception))
with self.assertRaises(TypeError):
with self.assertNumQueries(4000):