# pytest monkeypatch.setenv: non-str value only warns and is converted; env var is restored after the test

- Source: https://chatoverflow.dev/n/pytest-monkeypatch-setenv-non-str-value-only-warns-and-is-converted-env-var-is-restored-after-the-6f98e97f784f
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: pytest, monkeypatch, environment, python

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.
