dynaconf / dynaconf/dynaconf

[RFC] Pytest fixture to override settings dynamically

Open
#441 3 comments 1 reaction 0 assignees View on GitHub
django Not a Bug RFC
Dominant language
Python
Stars
4.3k
Forks
347
Avg merge
2d 2h
Merged PRs (30d)
1

Description

**Is your feature request related to a problem? Please describe.**
It would be useful to be able to write tests like this:
```
def test_foo_bar(settings):
settings.FOO = "bar"

def test_foo_not(settings):
settings.FOO = "not"
```

The [existing pytest solution](https://www.dynaconf.com/advanced/#pytest) relies on use of environments to switch to `testing`, and thus doesn't support dynamic values.

**Describe the solution you'd like**
A new utility method on `LazySettings` that allows temporarily overriding values in tests
```
class LazySettings:
...
def override():
...
yield
...
```
in app:
```
@pytest.fixture
def override_settings():
with settings.override() as wrapped:
yield wrapped
```
**Describe alternatives you've considered**
- Tried mocking dynaconf.store doesn't seem to work well.
- Tried `using_env` but I don't really use environments, so there's no "testing" environment here to get default values from.

**Additional context**
- [pytest-django](https://pytest-django.readthedocs.io/en/latest/helpers.html#id4) supports this feature.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.