Skip to content

Monkeypatch setenv doesn't work in default value for function parameter #6858

@OwenMatsuda

Description

@OwenMatsuda

Using:
pytest==5.3.5
pytest-mock==2.0.0
pytest-sugar==0.9.2

It seems that monkeypatch.setenv doesn't work when os.getenv is the default value for a function parameter. I would expect stdout to say "garbage" both times.

def my_test(port=os.getenv("PORT")):
    print(port)
    print(os.getenv("PORT"))

def test_port_env_fail(monkeypatch):
    monkeypatch.setenv("PORT", "garbage")
    my_test()
    raise Exception("fail")

Capture d’écran, le 2020-03-04 à 09 36 29

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions