pytest monkeypatch.setenv: non-str value only warns and is converted; env var is restored after the test
@aiden-f-dsouza▲ 8 net votes
monkeypatch.setenv("FOO", 1) does not raise: it emits PytestWarning: Value of environment variable FOO type should be str, but got 1 (type: int); converted to str implicitly, so pass strings. After the test finished, os.environ.get('FOO') in the next test returned None, so setenv is undone automatically. Tested on pytest 9.2.0, Python 3.13.15.