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

How to patch settings attributes after fixtures will be loaded but before apps imports #1058

Closed
mihalt opened this issue Apr 8, 2023 · 2 comments

Comments

@mihalt
Copy link

mihalt commented Apr 8, 2023

So, I have few strings in settings.py like

DELIVERED_FILES_DIR = os.path.join(MEDIA_ROOT, 'csv_files')
INVOICES_DIR = os.path.join(MEDIA_ROOT, 'invoices')

I use them in few parts of my app. When I created test, I wanted to patch them by manual like

@pytest.fixture
def change_paths(settings, tmp_path):
    settings.DELIVERED_FILES_DIR = tmp_path
    settings.INVOICES_DIR = tmp_path

But it does nothing because settings attributes where already imported before running a code from test, if I understand correct.

I tried to use

@pytest.hookimpl(tryfirst=True)
def pytest_load_initial_conftests(early_config, parser, settings, tmp_path, args):
    settings.DELIVERED_FILES_DIR = tmp_path
    settings.INVOICES_DIR = tmp_path

and got
Argument(s) {'settings', 'tmp_path'} are declared in the hookimpl but can not be found in the hookspec

With session scoped fixtures I got error lik
ScopeMismatch: You tried to access the function scoped fixtures settings, tmp_path with a session scoped request object, involved factories:

So, would like to know how can I achieve my goals.

@tony
Copy link
Member

tony commented Oct 29, 2023

I ran into this just now.

My niche anecdote with TEMPLATES

settings.TEMPLATES, post-initialization is effectively immutable.

Editing settings.TEMPLATES directly before rendering won't change what views use EngineHandler (its cached library/registry object)

I went from directly use DJANGO_SETTINGS_MODULE to going to pytest_configure to initialize settings, and I found that I can't use DIRS + tmp_path derived fixtures when running settings.configure().

The only choice may end up being having pre-determined .gitignore'd / temporary directory for writing temporary files to, but it may not end up being pytest's own temporary directory fixtures.

@mihalt
Copy link
Author

mihalt commented Oct 10, 2024

I ran into this just now.
My niche anecdote with TEMPLATES

I've found a problem that resolves my issue. I had imports of settings in my project like form settings_folder import settings. So, when I imported them directly like that, I didn't have patched settings by pytest-django.

This is not correct even by native django requirements. Import like from django.conf import settings is strictly important.
https://docs.djangoproject.com/en/5.1/topics/settings/#using-settings-in-python-code

@mihalt mihalt closed this as completed Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants