pytest-dev / pytest-dev/pytest-django

How to change environment variable before django settings will be initialized?

Open
#1,136 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

I try to use this, but in fact settings var that was initialized in docker container doesn't change.

conftest.py

@pytest.hookimpl(tryfirst=True)
def pytest_load_initial_conftests(early_config, parser, args):
    os.environ["CH_BASE_OF_URL"] = 'https://api.cowho.com'

settings.py

CH_BASE_URL = os.environ.get('CH_BASE_OF_URL')

I tried to use pytest_configure. It doesn't work too. Just passing of settings fixture to the test doesn't work for me, because initialization of data appears after django_db_blocker.unblock() by signal and depends from settings variable too.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the conftest.py pytest_load_initial_conftests hook and the settings.py environment lookup shown in the report. Reproduce the initialization order with pytest and django_db_blocker.unblock(), including the signal that creates data. Done means documenting or verifying a supported way to set the environment variable before Django settings and signal initialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
backend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.