Last active
December 24, 2015 23:19
-
-
Save stantonk/6879138 to your computer and use it in GitHub Desktop.
Print all SQL queries performed during a unittest in django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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