Djangoのテストで実行されたsqlを確認する
# coding=utf-8 from django.test import TestCase from django.db import connection from apps.models import Person class TestSample(TestCase): def test_sample(self): connection.use_debug_cursor = True connection.queries = [] # do something Person.objects.create(name="John", age=90) self.assertEquals(Person.objects.count(), 1) print connection.queries # [{u'time': u'0.000', u'sql': u'QUERY = u\'INSERT INTO "apps_person" ("name", "age") VALUES (%s, %s)\' - PARAMS = (u\'John\', 90)'}, # {u'time': u'0.000', u'sql': u'QUERY = u\'SELECT COUNT(*) FROM "apps_person"\' - PARAMS = ()'}] connection.use_debug_cursor = None
参考 : Django: is there a way to count SQL queries from an unit test? - Stack Overflow