pytest-djangoapp icon indicating copy to clipboard operation
pytest-djangoapp copied to clipboard

Nested settings context manager

Open jayvdb opened this issue 4 years ago • 4 comments

I'm not sure if this is going to be possible/easy

def test_foo(settings):
    with settings(FOO=True) as settings:
        with settings(BLAH=True):
            # Use FOO and BLAH
            ...

        with settings(BLAH=False):
            # Using FOO here fails
            ...

jayvdb avatar Jul 28 '20 13:07 jayvdb

And what'd be the use-case for that? In common case it'd be more vivid to (yet not so DRY) if we explicitly declare settings in one manager, and use parametrize to handle value variants.

idlesign avatar Jul 29 '20 00:07 idlesign

The example in the issue is not the best, and "nested" isnt the only scenario. It also occurs in non-nested usages.

https://github.com/idlesign/pytest-djangoapp/pull/15/files#diff-1349745828b4553381fb638e3ec9485fR74 shows that after a context manager __exit__, the settings fixture is effectively reset, so a simpler 'bug' is

def test_foo(settings):
    settings.FOO = True
    with settings(BAR=True) as settings:
        ...

    assert settings.FOO == True  # fails with AttributeError

wrt nested, and parameterized, I find sometimes parameterized is used when a test method which intermingles the parameterized values would allow better 'reading' - i.e. the flow of the test method is sometimes lost, and the meaning contorted, when parameterized is used. Not always, of course.

jayvdb avatar Jul 29 '20 02:07 jayvdb

The AttributeError above seems somewhat unexpected indeed, yet code appears to be somewhat synthetic.

idlesign avatar Jul 30 '20 07:07 idlesign

I encountered this issue because I am trying to use pytest-djangoapp for https://github.com/jayvdb/django-dunder/blob/master/django_dunder/tests/test_settings.py

Those test methods are more descriptive/readible because they incrementally add settings, starting with high level settings and showing what they do, and then adding more precise settings which alter the behaviour.

My initial attempts of using settings as a context manager didnt allow incrementally adding settings. Now I understand the problem of this issue, I need to re-attempt it as maybe I can achieve what I want within the constraints of how it works.

jayvdb avatar Jul 30 '20 08:07 jayvdb