I'm not using classes or test cases I'm just using pytest functions (want to keep it that way).
This does not work:
@pytest.fixture(scope="function")def set_env(): with MonkeyPatch.context() as mp: mp.setenv("VAR_ONE", "123") mp.setenv("VAR_TWO", "test")def test_blah(set_env): print(os.environ["VAR_ONE"]) print(os.environ["VAR_TWO"])
This does:
@pytest.fixture(scope="function")def set_env(monkeypatch): monkeypatch.setenv("VAR_ONE", "123") monkeypatch.setenv("VAR_TWO", "test")def test_blah(monkeypatch, set_env): print(os.environ["VAR_ONE"]) print(os.environ["VAR_TWO"])
I was hoping to avoid passing around monkeypatch fixtures like this. I thought I could use MonkeyPatch
to abstract this behind a single fixture. Am I misunderstanding MonkeyPatch
as a context manager?
The whole pytest fixture magic thing doesn't play nice with type hinting so I really want to minimize the fixtures I need to pass around (while still not using test cases and classes).