Skip to content

How to use dynaconf in pytest when declared custom settings object #728

@mzebrak

Description

@mzebrak

Hi. I need to override the environment for settings in conftest.py. As I found I can do it by adding:

from dynaconf import settings


@pytest.fixture(scope="session", autouse=True)
def set_test_settings():
    settings.configure(FORCE_ENV_FOR_DYNACONF="testing")

but in my case I' ve got my own settings object under in the confg.py module and it looks like this:

settings = Dynaconf(
    envvar_prefix='MY_PREFIX',
    settings_files=['settings.toml', '.secrets.toml'],
    environments=True,
    env_switcher='ENVIRONMENT_NAME',
)

and when I'm trying to do

from config import settings


@pytest.fixture(scope="session", autouse=True)
def set_test_settings():
    settings.configure(FORCE_ENV_FOR_DYNACONF="testing")

it does not work. It only overrides settings imported directly from dynaconf module. This is a problem because in my app I'm using settings from my config.py and my tests are failing when I override settings from dynaconf and use from dynaconf import settings in tests - because in app I'm using from config import settings

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions