Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not call django.setup() multiple times, test pytest_configure etc order #629

Merged
merged 1 commit into from
Aug 5, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,15 @@
'python': ('https://docs.python.org/3', None),
'django': ('https://docs.djangoproject.com/en/dev/',
'https://docs.djangoproject.com/en/dev/_objects/'),
'pytest': ('https://docs.pytest.org/en/latest/', None),
}


def setup(app):
# Allow linking to pytest's confvals.
app.add_description_unit(
"confval",
"pytest-confval",
objname="configuration value",
indextemplate="pair: %s; configuration value",
)
19 changes: 19 additions & 0 deletions docs/configuring_django.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,22 @@ This can be done from your project's ``conftest.py`` file::
def pytest_configure():
settings.configure(DATABASES=...)

Changing your app before Django gets set up
-------------------------------------------

pytest-django calls :py:func:`django.setup` automatically. If you want to do
anything before this, you have to create a pytest plugin and use
the :py:func:`~_pytest.hookspec.pytest_load_initial_conftests` hook, with
``tryfirst=True``, so that it gets run before the hook in pytest-django
itself::

@pytest.hookimpl(tryfirst=True)
def pytest_load_initial_conftests(early_config, parser, args):
import project.app.signals

def noop(*args, **kwargs):
pass

project.app.signals.something = noop

This plugin can then be used e.g. via ``-p`` in :pytest-confval:`addopts`.
5 changes: 4 additions & 1 deletion pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ def _setup_django():
if not django.conf.settings.configured:
return

django.setup()
import django.apps
if not django.apps.apps.ready:
django.setup()

_blocking_manager.block()


Expand Down
50 changes: 50 additions & 0 deletions tests/test_initialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from textwrap import dedent


def test_django_setup_order_and_uniqueness(django_testdir, monkeypatch):
"""
The django.setup() function shall not be called multiple times by
pytest-django, since it resets logging conf each time.
"""
django_testdir.makeconftest('''
import django.apps
assert django.apps.apps.ready
from tpkg.app.models import Item

print("conftest")
def pytest_configure():
import django
print("pytest_configure: conftest")
django.setup = lambda: SHOULD_NOT_GET_CALLED
''')

django_testdir.project_root.join('tpkg', 'plugin.py').write(dedent('''
import pytest
import django.apps
assert not django.apps.apps.ready

print("plugin")
def pytest_configure():
assert django.apps.apps.ready
from tpkg.app.models import Item
print("pytest_configure: plugin")

@pytest.hookimpl(tryfirst=True)
def pytest_load_initial_conftests(early_config, parser, args):
print("pytest_load_initial_conftests")
assert not django.apps.apps.ready
'''))
django_testdir.makepyfile("""
def test_ds():
pass
""")
result = django_testdir.runpytest_subprocess('-s', '-p', 'tpkg.plugin')
result.stdout.fnmatch_lines([
'plugin',
'pytest_load_initial_conftests',
'conftest',
'pytest_configure: conftest',
'pytest_configure: plugin',
'*1 passed*',
])
assert result.ret == 0