Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

Unreleased
----------

Features
^^^^^^^^
* Added a new option `--django-debug` to set the DEBUG setting to True prior to
running tests.

3.1.2
-----

Expand Down
12 changes: 12 additions & 0 deletions docs/configuring_django.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 --django-debug flag in command line.

Command Line Option::

$ pytest --django-debug

13 changes: 11 additions & 2 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +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('--django-debug',
action='store_true', dest='debug_mode', default=False,
help='Set DEBUG to true prior to run tests')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (no need to change it already): s/true/True ?!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change it. Thanks.

parser.addini(CONFIGURATION_ENV,
'django-configurations class to use by pytest-django.')
group._addoption('--liveserver', default=None,
Expand Down Expand Up @@ -332,11 +335,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)
dj_settings.DEBUG = False
setup_test_environment()
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)


Expand Down
24 changes: 24 additions & 0 deletions tests/test_django_settings_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('--django-debug')
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="""
Expand Down