-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixup Django settings init for tests
- Was running into some buggy behavior with pytest-django's autouse session fixture; need to override and disable their setup completely
- Loading branch information
Showing
2 changed files
with
33 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters