Skip to content

Commit

Permalink
Fixup Django settings init for tests
Browse files Browse the repository at this point in the history
- Was running into some buggy behavior with pytest-django's autouse
session fixture; need to override and disable their setup completely
  • Loading branch information
joshuatz committed Nov 3, 2024
1 parent fa7ca29 commit 8a5aca9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
49 changes: 32 additions & 17 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import pytest
from django.conf import global_settings, settings
from django.test.utils import setup_test_environment

pytest_plugins = ["pytester", "django_utils_lib.testing.pytest_plugin"]

# Set up dummy settings for any tests that rely on Django internals
# being able to bootstrap against settings.py
settings.configure(default_settings=global_settings)
settings.DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
}
settings.INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]

def configure_django_settings():
if settings.configured:
return
settings.configure(
default_settings=global_settings,
DATABASES={
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
},
INSTALLED_APPS=[
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
],
)


def pytest_configure():
# TODO: This is rather hacky and it would be great to find a cleaner way to
# do this. However, the pytest-django fixture we are disabling here is hard
# to alter in any other way, especially as it uses session-level autouse
pytest.MonkeyPatch().setattr("pytest_django.plugin.django_test_environment", lambda: None)
configure_django_settings()
setup_test_environment(debug=False)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ testpaths = [
"django_utils_lib/tests"
]
pythonpath = ". django_utils_lib"
django_find_project = false
auto_debug = true
auto_debug_wait_for_connect = false

Expand Down

0 comments on commit 8a5aca9

Please sign in to comment.