Fixed #21462 -- Made `assertNumQueries` print executed queries on failure.
This commit is contained in:
parent
75924cfa6d
commit
5cd6477fd6
|
@ -103,8 +103,12 @@ class _AssertNumQueriesContext(CaptureQueriesContext):
|
||||||
return
|
return
|
||||||
executed = len(self)
|
executed = len(self)
|
||||||
self.test_case.assertEqual(
|
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
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -713,6 +713,9 @@ Miscellaneous
|
||||||
``select_related('foo').select_related('bar')``. Previously the latter would
|
``select_related('foo').select_related('bar')``. Previously the latter would
|
||||||
have been equivalent to ``select_related('bar')``.
|
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
|
Features deprecated in 1.7
|
||||||
==========================
|
==========================
|
||||||
|
|
||||||
|
|
|
@ -192,6 +192,7 @@ class AssertNumQueriesContextManagerTests(TestCase):
|
||||||
with self.assertNumQueries(2):
|
with self.assertNumQueries(2):
|
||||||
Person.objects.count()
|
Person.objects.count()
|
||||||
self.assertIn("1 queries executed, 2 expected", str(exc_info.exception))
|
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.assertRaises(TypeError):
|
||||||
with self.assertNumQueries(4000):
|
with self.assertNumQueries(4000):
|
||||||
|
|
Loading…
Reference in New Issue