From a074460d35a90082651a06bcb415aba3ade70ab2 Mon Sep 17 00:00:00 2001 From: Pierre Dulac Date: Mon, 20 Feb 2017 13:33:49 +0100 Subject: [PATCH 1/7] Implement the --django-debug plugin option with tests --- docs/changelog.rst | 9 +++- docs/configuring_django.rst | 14 ++++++ pytest_django/plugin.py | 12 ++++- tests/test_django_settings_module.py | 75 ++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index b1a972e86..1d0fd2662 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,5 +1,12 @@ Changelog -========= + +Unreleased +---------- + +Bugfixes +^^^^^^^^ + +* Added a new option `--django-debug` to specify the default DEBUG setting (#228) 3.4.8 (2019-02-26) ------------------ diff --git a/docs/configuring_django.rst b/docs/configuring_django.rst index 21f4debf9..73c1d4196 100644 --- a/docs/configuring_django.rst +++ b/docs/configuring_django.rst @@ -104,3 +104,17 @@ itself:: project.app.signals.something = noop This plugin can then be used e.g. via ``-p`` in :pytest-confval:`addopts`. + +``DEBUG`` setting during the test run +------------------------------------- + +By default, django test runner behavior is to force DEBUG setting to False. So does the ``pytest-django``. +But sometimes, especially for functional tests, you might want to avoid this, to debug why certain page does not work. + +Command Line Option:: + + $ py.test --django-debug True|False|None + +``None`` ensure there is no override of the test settings DEBUG value +``True`` override DEBUG to True +``False`` override DEBUG to False diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 09c61c801..943ce5e4d 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -5,6 +5,7 @@ """ import contextlib +from distutils.util import strtobool import inspect from functools import reduce import os @@ -106,6 +107,13 @@ def pytest_addoption(parser): default=False, help="Enable Django migrations on test setup", ) + group._addoption( + "--django-debug", + action="store", + dest="djangodebug", + default="None", + help="Configure the DEBUG setting. Defaults to False" + ) parser.addini( CONFIGURATION_ENV, "django-configurations class to use by pytest-django." ) @@ -466,7 +474,9 @@ 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 + if request.config.getvalue('djangodebug') != 'None': + dj_settings.DEBUG = bool(strtobool(request.config.getvalue('djangodebug'))) + setup_test_environment() request.addfinalizer(teardown_test_environment) diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index 09b8427e8..3fcb5875d 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -308,6 +308,81 @@ def test_debug_is_false(): assert r.ret == 0 +def test_no_debug_override(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_unchanged(): + assert settings.DEBUG is True + """) + + r = testdir.runpytest_subprocess('--django-debug=None') + assert r.ret == 0 + + +def test_override_debug_to_false(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_false(): + assert settings.DEBUG is False + """) + + r = testdir.runpytest_subprocess('--django-debug=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('--django-debug=True') + assert r.ret == 0 + + @pytest.mark.skipif( not hasattr(django, "setup"), reason="This Django version does not support app loading", From 36c9a0d1fe7cb8b878620ee8d96d9e06b3dac337 Mon Sep 17 00:00:00 2001 From: Pierre Dulac Date: Sat, 2 Dec 2017 12:23:50 +0100 Subject: [PATCH 2/7] Follow the new convention for Django 11.0 a new option --debug-mode has been introduced, so let's name ours --django-debug-mode --- docs/changelog.rst | 2 +- docs/configuring_django.rst | 2 +- pytest_django/plugin.py | 8 ++++---- tests/test_django_settings_module.py | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 1d0fd2662..481571339 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,7 +6,7 @@ Unreleased Bugfixes ^^^^^^^^ -* Added a new option `--django-debug` to specify the default DEBUG setting (#228) +* Added a new option `--django-debug-mode` to specify the default DEBUG setting (#228) 3.4.8 (2019-02-26) ------------------ diff --git a/docs/configuring_django.rst b/docs/configuring_django.rst index 73c1d4196..258e33437 100644 --- a/docs/configuring_django.rst +++ b/docs/configuring_django.rst @@ -113,7 +113,7 @@ But sometimes, especially for functional tests, you might want to avoid this, to Command Line Option:: - $ py.test --django-debug True|False|None + $ py.test --django-debug-mode True|False|None ``None`` ensure there is no override of the test settings DEBUG value ``True`` override DEBUG to True diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 943ce5e4d..14bf37119 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -108,9 +108,9 @@ def pytest_addoption(parser): help="Enable Django migrations on test setup", ) group._addoption( - "--django-debug", + "--django-debug-mode", action="store", - dest="djangodebug", + dest="djangodebugmode", default="None", help="Configure the DEBUG setting. Defaults to False" ) @@ -474,8 +474,8 @@ def django_test_environment(request): from django.conf import settings as dj_settings from django.test.utils import setup_test_environment, teardown_test_environment - if request.config.getvalue('djangodebug') != 'None': - dj_settings.DEBUG = bool(strtobool(request.config.getvalue('djangodebug'))) + if request.config.getvalue('djangodebugmode') != 'None': + dj_settings.DEBUG = bool(strtobool(request.config.getvalue('djangodebugmode'))) setup_test_environment() request.addfinalizer(teardown_test_environment) diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index 3fcb5875d..4f89460c2 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -329,7 +329,7 @@ def test_debug_is_unchanged(): assert settings.DEBUG is True """) - r = testdir.runpytest_subprocess('--django-debug=None') + r = testdir.runpytest_subprocess('--django-debug-mode=None') assert r.ret == 0 @@ -354,7 +354,7 @@ def test_debug_is_false(): assert settings.DEBUG is False """) - r = testdir.runpytest_subprocess('--django-debug=False') + r = testdir.runpytest_subprocess('--django-debug-mode=False') assert r.ret == 0 @@ -379,7 +379,7 @@ def test_debug_is_true(): assert settings.DEBUG is True """) - r = testdir.runpytest_subprocess('--django-debug=True') + r = testdir.runpytest_subprocess('--django-debug-mode=True') assert r.ret == 0 From 5b4baa11a8bcaa034283cdedf1f2489a19904628 Mon Sep 17 00:00:00 2001 From: Pierre Dulac Date: Sat, 2 Dec 2017 12:30:33 +0100 Subject: [PATCH 3/7] Use the Django 1.11 setup api if available --- pytest_django/plugin.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 14bf37119..967a18122 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -471,13 +471,21 @@ 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 if request.config.getvalue('djangodebugmode') != 'None': - dj_settings.DEBUG = bool(strtobool(request.config.getvalue('djangodebugmode'))) - - setup_test_environment() + django_debug_mode = bool(strtobool(request.config.getvalue('djangodebugmode'))) + if StrictVersion(django.get_version()) >= StrictVersion('1.11'): + setup_test_environment(debug=django_debug_mode) + else: + dj_settings.DEBUG = django_debug_mode + setup_test_environment() + else: + # default setup + setup_test_environment() request.addfinalizer(teardown_test_environment) From b27a6eca7602fe47c891f6199c9933652bd943db Mon Sep 17 00:00:00 2001 From: Pierre Dulac Date: Fri, 24 May 2019 07:53:21 +0200 Subject: [PATCH 4/7] Fixed review comments about django version comparison --- pytest_django/plugin.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 967a18122..1a4cdb9c9 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -112,7 +112,7 @@ def pytest_addoption(parser): action="store", dest="djangodebugmode", default="None", - help="Configure the DEBUG setting. Defaults to False" + help="Configure the DEBUG setting. Defaults to None." ) parser.addini( CONFIGURATION_ENV, "django-configurations class to use by pytest-django." @@ -471,14 +471,13 @@ 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 - if request.config.getvalue('djangodebugmode') != 'None': - django_debug_mode = bool(strtobool(request.config.getvalue('djangodebugmode'))) - if StrictVersion(django.get_version()) >= StrictVersion('1.11'): + if request.config.getvalue("djangodebugmode") != "None": + django_debug_mode = bool(strtobool(request.config.getvalue("djangodebugmode"))) + if django.VERSION >= (1, 11): setup_test_environment(debug=django_debug_mode) else: dj_settings.DEBUG = django_debug_mode From 0226b5ba53fc20d33bd8c5d964470d6e1b308bbe Mon Sep 17 00:00:00 2001 From: Pierre Dulac Date: Fri, 24 May 2019 08:04:50 +0200 Subject: [PATCH 5/7] Remove previous debug test, not needed anymore --- tests/test_django_settings_module.py | 31 +--------------------------- 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index 4f89460c2..4c6ab5aa0 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -279,35 +279,6 @@ def test_settings(): assert result.ret == 0 -def test_debug_false(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_false(): - assert settings.DEBUG is False - """ - ) - - r = testdir.runpytest_subprocess() - assert r.ret == 0 - - def test_no_debug_override(testdir, monkeypatch): monkeypatch.delenv('DJANGO_SETTINGS_MODULE') testdir.makeconftest(""" @@ -365,7 +336,7 @@ def test_override_debug_to_true(testdir, monkeypatch): def pytest_configure(): settings.configure(SECRET_KEY='set from pytest_configure', - DEBUG=True, + DEBUG=False, DATABASES={'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}}, From 5571a867f2accbb7fa1a0f8ac1424cef9be703f8 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 30 May 2019 18:36:50 +0200 Subject: [PATCH 6/7] revisit --- docs/changelog.rst | 4 ++-- docs/configuring_django.rst | 15 +++++++-------- pytest_django/plugin.py | 25 +++++++++++++------------ 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 481571339..9e4f72dc9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -3,10 +3,10 @@ Changelog Unreleased ---------- -Bugfixes +Features ^^^^^^^^ -* Added a new option `--django-debug-mode` to specify the default DEBUG setting (#228) +* Add a new option `--django-debug-mode` to specify the default DEBUG setting (#228) 3.4.8 (2019-02-26) ------------------ diff --git a/docs/configuring_django.rst b/docs/configuring_django.rst index 258e33437..06ca399dd 100644 --- a/docs/configuring_django.rst +++ b/docs/configuring_django.rst @@ -108,13 +108,12 @@ This plugin can then be used e.g. via ``-p`` in :pytest-confval:`addopts`. ``DEBUG`` setting during the test run ------------------------------------- -By default, django test runner behavior is to force DEBUG setting to False. So does the ``pytest-django``. -But sometimes, especially for functional tests, you might want to avoid this, to debug why certain page does not work. +Django's test runner forces ``DEBUG`` to be ``False`` by default, and so +does ``pytest-django``. +This can be controlled using the ``--django-debug-mode`` command line option:: -Command Line Option:: - - $ py.test --django-debug-mode True|False|None + $ pytest --django-debug-mode True|False|None -``None`` ensure there is no override of the test settings DEBUG value -``True`` override DEBUG to True -``False`` override DEBUG to False +``None`` do not override the default +``True`` override ``DEBUG`` to be ``True`` +``False`` override ``DEBUG`` to be ``False`` diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 1a4cdb9c9..1e49d5c4e 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -110,9 +110,9 @@ def pytest_addoption(parser): group._addoption( "--django-debug-mode", action="store", - dest="djangodebugmode", + dest="django_debug_mode", default="None", - help="Configure the DEBUG setting. Defaults to None." + help="Configure Django's DEBUG setting." ) parser.addini( CONFIGURATION_ENV, "django-configurations class to use by pytest-django." @@ -471,20 +471,21 @@ def django_test_environment(request): """ if django_settings_is_configured(): _setup_django() - import django - from django.conf import settings as dj_settings from django.test.utils import setup_test_environment, teardown_test_environment - if request.config.getvalue("djangodebugmode") != "None": - django_debug_mode = bool(strtobool(request.config.getvalue("djangodebugmode"))) + setup_test_environment_kwargs = {} + + django_debug_mode = request.config.getvalue("django_debug_mode") + if django_debug_mode != "None": + import django + debug = bool(strtobool(django_debug_mode)) if django.VERSION >= (1, 11): - setup_test_environment(debug=django_debug_mode) + setup_test_environment_kwargs["debug"] = debug else: - dj_settings.DEBUG = django_debug_mode - setup_test_environment() - else: - # default setup - setup_test_environment() + from django.conf import settings as dj_settings + dj_settings.DEBUG = debug + + setup_test_environment(**setup_test_environment_kwargs) request.addfinalizer(teardown_test_environment) From b64c7954f0ce53696bdd5b53f4425f12d9d19040 Mon Sep 17 00:00:00 2001 From: Pierre Dulac Date: Sun, 14 Jul 2019 18:39:26 +0200 Subject: [PATCH 7/7] Change django-debug-mode to django-debug --- docs/changelog.rst | 2 +- docs/configuring_django.rst | 4 ++-- pytest_django/plugin.py | 10 +++++----- tests/test_django_settings_module.py | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 9e4f72dc9..54f198dc2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,7 +6,7 @@ Unreleased Features ^^^^^^^^ -* Add a new option `--django-debug-mode` to specify the default DEBUG setting (#228) +* Add a new option `--django-debug` to specify the default DEBUG setting (#228) 3.4.8 (2019-02-26) ------------------ diff --git a/docs/configuring_django.rst b/docs/configuring_django.rst index 06ca399dd..d5fbad2c6 100644 --- a/docs/configuring_django.rst +++ b/docs/configuring_django.rst @@ -110,9 +110,9 @@ This plugin can then be used e.g. via ``-p`` in :pytest-confval:`addopts`. Django's test runner forces ``DEBUG`` to be ``False`` by default, and so does ``pytest-django``. -This can be controlled using the ``--django-debug-mode`` command line option:: +This can be controlled using the ``--django-debug`` command line option:: - $ pytest --django-debug-mode True|False|None + $ pytest --django-debug True|False|None ``None`` do not override the default ``True`` override ``DEBUG`` to be ``True`` diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 1e49d5c4e..9b9658d22 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -108,9 +108,9 @@ def pytest_addoption(parser): help="Enable Django migrations on test setup", ) group._addoption( - "--django-debug-mode", + "--django-debug", action="store", - dest="django_debug_mode", + dest="django_debug", default="None", help="Configure Django's DEBUG setting." ) @@ -475,10 +475,10 @@ def django_test_environment(request): setup_test_environment_kwargs = {} - django_debug_mode = request.config.getvalue("django_debug_mode") - if django_debug_mode != "None": + django_debug = request.config.getvalue("django_debug") + if django_debug != "None": import django - debug = bool(strtobool(django_debug_mode)) + debug = bool(strtobool(django_debug)) if django.VERSION >= (1, 11): setup_test_environment_kwargs["debug"] = debug else: diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index 4c6ab5aa0..3a970f353 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -300,7 +300,7 @@ def test_debug_is_unchanged(): assert settings.DEBUG is True """) - r = testdir.runpytest_subprocess('--django-debug-mode=None') + r = testdir.runpytest_subprocess('--django-debug=None') assert r.ret == 0 @@ -325,7 +325,7 @@ def test_debug_is_false(): assert settings.DEBUG is False """) - r = testdir.runpytest_subprocess('--django-debug-mode=False') + r = testdir.runpytest_subprocess('--django-debug=False') assert r.ret == 0 @@ -350,7 +350,7 @@ def test_debug_is_true(): assert settings.DEBUG is True """) - r = testdir.runpytest_subprocess('--django-debug-mode=True') + r = testdir.runpytest_subprocess('--django-debug=True') assert r.ret == 0