diff --git a/CHANGELOG.md b/CHANGELOG.md index 46c4c727..c323fb1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Now defaults to 3.4.9 for Tailwind CSS version. - Now defaults to 1.46.0 for Playwright version. - Now defaults to v2024.8.27 for `django-twc-ui` version. +- Instead of using a dictionary of settings specific to tests with `django.test.utils.override_settings`, there is now a dedicated `tests/settings.py` that imports the project's main `settings.py` and overrides with test specific ones. When trying to use `django-q2` and `pytest-xdist`, there seems to be a bug between the two when also using `override_settings`. The override for sync mode in q2 does not seem to be respected. This has led to inconsistent behavior when testing parts of the project that use `async_task` from q2 to offload tasks to the background, e.g. sending email via an `async_task` and not being able to check the `mailoutbox`. ### Fixed diff --git a/src/django_twc_project/pyproject.toml.jinja b/src/django_twc_project/pyproject.toml.jinja index cb1da8bc..143468e5 100644 --- a/src/django_twc_project/pyproject.toml.jinja +++ b/src/django_twc_project/pyproject.toml.jinja @@ -203,7 +203,7 @@ module = [ ignore_missing_model_attributes = true [tool.pytest.ini_options] -DJANGO_SETTINGS_MODULE = "{{ module_name }}.settings" +DJANGO_SETTINGS_MODULE = "tests.settings" addopts = "--reuse-db -n auto --dist loadfile --cov={{ module_name }} --cov-report= --cov-config=pyproject.toml" norecursedirs = ".* bin build dist *.egg htmlcov logs node_modules static templates venv" python_files = "tests.py test_*.py *_tests.py" diff --git a/src/django_twc_project/tests/conftest.py b/src/django_twc_project/tests/conftest.py index 291294ac..d0bf72bb 100644 --- a/src/django_twc_project/tests/conftest.py +++ b/src/django_twc_project/tests/conftest.py @@ -1,45 +1,11 @@ from __future__ import annotations import logging +import os -import pytest -from django.test.utils import override_settings - -pytest_plugins = [] # type: ignore +pytest_plugins = [] def pytest_configure(config): logging.disable(logging.CRITICAL) - - -TEST_SETTINGS = { - "DEBUG": False, - "CACHES": { - "default": { - "BACKEND": "django.core.cache.backends.dummy.DummyCache", - } - }, - "EMAIL_BACKEND": "django.core.mail.backends.locmem.EmailBackend", - "LOGGING_CONFIG": None, - "PASSWORD_HASHERS": [ - "django.contrib.auth.hashers.MD5PasswordHasher", - ], - "Q_CLUSTER": { - "sync": True, - }, - "STORAGES": { - "default": { - "BACKEND": "django.core.files.storage.InMemoryStorage", - }, - "staticfiles": { - "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", - }, - }, - "WHITENOISE_AUTOREFRESH": True, -} - - -@pytest.fixture(autouse=True, scope="session") -def use_test_settings(): - with override_settings(**TEST_SETTINGS): - yield + os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings" diff --git a/src/django_twc_project/tests/settings.py.jinja b/src/django_twc_project/tests/settings.py.jinja new file mode 100644 index 00000000..1466f257 --- /dev/null +++ b/src/django_twc_project/tests/settings.py.jinja @@ -0,0 +1,34 @@ +from __future__ import annotations + +from django.conf import settings + +from {{ module_name }}.settings import * # noqa: F403 + +DEBUG = False + +CACHES = { + "default": { + "BACKEND": "django.core.cache.backends.dummy.DummyCache", + } +} + +EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" + +LOGGING_CONFIG = None + +PASSWORD_HASHERS = [ + "django.contrib.auth.hashers.MD5PasswordHasher", +] + +Q_CLUSTER = settings.Q_CLUSTER | {"sync": True} + +STORAGES = { + "default": { + "BACKEND": "django.core.files.storage.InMemoryStorage", + }, + "staticfiles": { + "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", + }, +} + +WHITENOISE_AUTOREFRESH = True