Created
May 28, 2020 10:01
-
-
Save fopina/6b6adf195c41a287da421dda5b73aaa6 to your computer and use it in GitHub Desktop.
pytest-django --debug-sql
This file contains 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
__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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If no logging handler is configed in settings.py, then an logging handler is required.