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

change from overriding settings to explicit settings module for tests #300

Merged
merged 4 commits into from
Aug 12, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/django_twc_project/pyproject.toml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 3 additions & 37 deletions src/django_twc_project/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 34 additions & 0 deletions src/django_twc_project/tests/settings.py.jinja
Original file line number Diff line number Diff line change
@@ -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