To log SQL queries during testing, you need two things:
django.db.backends
logger enabled and
@override_settings(DEBUG=True)
decorator.
Test runner will set DEBUG=False by default, ignoring what you may have set in DJANGO_SETTINGS_MODULE.
The minimum settings:
# https://docs.djangoproject.com/en/dev/ref/settings/#logging
LOGGING = {
'version': 1,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.db.backends': {
'level': 'DEBUG',
},
},
'root': {
'handlers': ['console'],
}
}
The example test case:
from django.contrib.auth.models import User
from django.test import TestCase, override_settings
class UserTests(TestCase):
# To log queries in tests you need to manually override DEBUG setting
# because testing sets DEBUG=False by default
@override_settings(DEBUG=True)
def test_create_user(self):
User.objects.create()