Skip to content

Instantly share code, notes, and snippets.

@stantonk
Last active December 24, 2015 23:19
Show Gist options
  • Save stantonk/6879138 to your computer and use it in GitHub Desktop.
Save stantonk/6879138 to your computer and use it in GitHub Desktop.
Print all SQL queries performed during a unittest in django
from django.conf import settings
from django.db import connections
#TODO: patch run method instead?
#TODO: print formatting
class LogSqlQueriesTestCase(TestCase):
@classmethod
def setUpClass(cls):
# must override for queries to be recorded
cls._old_settings_DEBUG = settings.DEBUG
settings.DEBUG = True
super(LogSqlQueriesTestCase, cls).setUpClass()
def tearDown(self):
print '-'*80
for c in connections:
for q in connections[c].queries:
print '%s: %s' % (c, q['sql'])
connections[c].queries = []
super(LogSqlQueriesTestCase, self).tearDown()
@classmethod
def tearDownClass(cls):
settings.DEBUG = cls._old_settings_DEBUG
super(LogSqlQueriesTestCase, cls).tearDownClass()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment