From c4e13ca188c71bba1bfe1a6d2233845377e6dfeb Mon Sep 17 00:00:00 2001 From: Jonghyun Park Date: Wed, 9 Aug 2017 14:46:35 +0900 Subject: [PATCH 1/4] Implement --debug-mode option to set DEBUG to True prior to run tests --- docs/changelog.rst | 8 ++++++++ docs/configuring_django.rst | 12 ++++++++++++ pytest_django/plugin.py | 6 ++++-- tests/test_django_settings_module.py | 24 ++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index b9dcf20de..e593acbeb 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,14 @@ Changelog ========= +Unreleased +---------- + +Features +^^^^^^^^ +* Added a new option `--debug-mode` to set the DEBUG setting to True prior to + running tests. + 3.1.2 ----- diff --git a/docs/configuring_django.rst b/docs/configuring_django.rst index e43dd8688..2645138a4 100644 --- a/docs/configuring_django.rst +++ b/docs/configuring_django.rst @@ -83,3 +83,15 @@ This can be done from your project's ``conftest.py`` file:: def pytest_configure(): settings.configure(DATABASES=...) +``DEBUG`` setting during the test run +------------------------------------- + +By default, django test runner forces `DEBUG` setting to `False`, and so does +the ``pytest-django``. But sometimes, especially for functional tests, there is +a need to set `DEBUG` to `True` to investigate why certain page does not work. +To do so, set --debug-mode flag in command line. + +Command Line Option:: + + $ pytest --debug-mode + diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index a71fc1070..1327e386e 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -67,6 +67,9 @@ def pytest_addoption(parser): group._addoption('--migrations', action='store_false', dest='nomigrations', default=False, help='Enable Django 1.7+ migrations on test setup') + group._addoption('--debug-mode', + action='store_true', dest='debug_mode', default=False, + help='Set DEBUG to true prior to run tests') parser.addini(CONFIGURATION_ENV, 'django-configurations class to use by pytest-django.') group._addoption('--liveserver', default=None, @@ -336,8 +339,7 @@ def django_test_environment(request): from django.conf import settings as dj_settings from django.test.utils import (setup_test_environment, teardown_test_environment) - dj_settings.DEBUG = False - setup_test_environment() + setup_test_environment(debug=request.config.getvalue('debug_mode')) request.addfinalizer(teardown_test_environment) diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index a2c99300a..4301550dd 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -275,6 +275,30 @@ def test_debug_is_false(): assert r.ret == 0 +def test_override_debug_to_true(testdir, monkeypatch): + monkeypatch.delenv('DJANGO_SETTINGS_MODULE') + testdir.makeconftest(""" + from django.conf import settings + def pytest_configure(): + settings.configure(SECRET_KEY='set from pytest_configure', + DEBUG=True, + DATABASES={'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:'}}, + INSTALLED_APPS=['django.contrib.auth', + 'django.contrib.contenttypes',]) + """) + + testdir.makepyfile(""" + from django.conf import settings + def test_debug_is_true(): + assert settings.DEBUG is True + """) + + r = testdir.runpytest_subprocess('--debug-mode') + assert r.ret == 0 + + @pytest.mark.skipif(not hasattr(django, 'setup'), reason="This Django version does not support app loading") @pytest.mark.django_project(extra_settings=""" From 4dbc72049177bb5aa2433d6d721622e7df1b9d5f Mon Sep 17 00:00:00 2001 From: Jonghyun Park Date: Wed, 9 Aug 2017 16:34:13 +0900 Subject: [PATCH 2/4] Make debug mode work for Django<1.11 --- pytest_django/plugin.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 1327e386e..d5801ba2f 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -336,10 +336,17 @@ def django_test_environment(request): """ if django_settings_is_configured(): _setup_django() + from distutils.version import StrictVersion + import django from django.conf import settings as dj_settings from django.test.utils import (setup_test_environment, teardown_test_environment) - setup_test_environment(debug=request.config.getvalue('debug_mode')) + debug_mode = request.config.getvalue('debug_mode') + if StrictVersion(django.get_version()) >= StrictVersion('1.11'): + setup_test_environment(debug=debug_mode) + else: + dj_settings.DEBUG = debug_mode + setup_test_environment() request.addfinalizer(teardown_test_environment) From 59b052210b68e8606717ca936b4de7c26caeb5ac Mon Sep 17 00:00:00 2001 From: Jonghyun Park Date: Thu, 2 Nov 2017 00:42:37 +0900 Subject: [PATCH 3/4] Change --debug-mode flag to --django-debug --- docs/changelog.rst | 2 +- docs/configuring_django.rst | 4 ++-- pytest_django/plugin.py | 4 ++-- tests/test_django_settings_module.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index e593acbeb..2531b4bd7 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,7 +6,7 @@ Unreleased Features ^^^^^^^^ -* Added a new option `--debug-mode` to set the DEBUG setting to True prior to +* Added a new option `--django-debug` to set the DEBUG setting to True prior to running tests. 3.1.2 diff --git a/docs/configuring_django.rst b/docs/configuring_django.rst index 2645138a4..ef3d696d7 100644 --- a/docs/configuring_django.rst +++ b/docs/configuring_django.rst @@ -89,9 +89,9 @@ This can be done from your project's ``conftest.py`` file:: By default, django test runner forces `DEBUG` setting to `False`, and so does the ``pytest-django``. But sometimes, especially for functional tests, there is a need to set `DEBUG` to `True` to investigate why certain page does not work. -To do so, set --debug-mode flag in command line. +To do so, set --django-debug flag in command line. Command Line Option:: - $ pytest --debug-mode + $ pytest --django-debug diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index dd254b6aa..f176d5dbb 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -66,9 +66,9 @@ def pytest_addoption(parser): group._addoption('--migrations', action='store_false', dest='nomigrations', default=False, help='Enable Django migrations on test setup') - group._addoption('--debug-mode', - help='Set DEBUG to true prior to run tests') + group._addoption('--django-debug', action='store_true', dest='debug_mode', default=False, + help='Set DEBUG to true prior to run tests') parser.addini(CONFIGURATION_ENV, 'django-configurations class to use by pytest-django.') group._addoption('--liveserver', default=None, diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index 32e8d7cc3..b9ff45ea0 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -295,7 +295,7 @@ def test_debug_is_true(): assert settings.DEBUG is True """) - r = testdir.runpytest_subprocess('--debug-mode') + r = testdir.runpytest_subprocess('--django-debug') assert r.ret == 0 From b0e5f112e014b670002a35ec98894c6f21afcc57 Mon Sep 17 00:00:00 2001 From: Jonghyun Park Date: Thu, 2 Nov 2017 16:38:15 +0900 Subject: [PATCH 4/4] Fix minor type --- pytest_django/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index f176d5dbb..52d4a4259 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -68,7 +68,7 @@ def pytest_addoption(parser): help='Enable Django migrations on test setup') group._addoption('--django-debug', action='store_true', dest='debug_mode', default=False, - help='Set DEBUG to true prior to run tests') + help='Set DEBUG to True prior to run tests') parser.addini(CONFIGURATION_ENV, 'django-configurations class to use by pytest-django.') group._addoption('--liveserver', default=None,