Skip to content

Instantly share code, notes, and snippets.

@fopina
Created May 28, 2020 10:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fopina/6b6adf195c41a287da421dda5b73aaa6 to your computer and use it in GitHub Desktop.
Save fopina/6b6adf195c41a287da421dda5b73aaa6 to your computer and use it in GitHub Desktop.
pytest-django --debug-sql
__force_django_debug = False
def pytest_runtest_setup():
if __force_django_debug:
# required for logging SQL as pytest always sets DEBUG to False (why?)
# https://github.com/pytest-dev/pytest-django/blob/master/pytest_django/plugin.py#L473
from django.conf import settings
settings.DEBUG = True
def pytest_addoption(parser):
parser.addoption("--debug-sql", action="store_true", help="Prints logged SQL queries on failure.")
def pytest_configure(config):
if config.getoption("--debug-sql"):
global __force_django_debug
import logging
logging.getLogger('django.db.backends').setLevel(logging.DEBUG)
__force_django_debug = True
@jayvynl
Copy link

jayvynl commented Oct 19, 2022

If no logging handler is configed in settings.py, then an logging handler is required.

dblog = logging.getLogger('django.db.backends')
dblog.setLevel(logging.DEBUG)
dblog.addHandler(logging.StreamHandler())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment