diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 363172110d3..8d9d2777664 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -367,6 +367,21 @@ def test_get_user(monkeypatch): assert get_user() is None +@pytest.mark.skipif(not sys.platform.startswith("win"), reason="win only") +def test_get_user_handles_oserror(monkeypatch): + """Test that get_user() returns None when getpass.getuser() raises OSError. + + This handles the Python 3.13+ behavior where getpass.getuser() raises + OSError when no username environment variables (LOGNAME, USER, LNAME, + USERNAME) are set on Windows (#11874). + """ + monkeypatch.delenv("LOGNAME", raising=False) + monkeypatch.delenv("USER", raising=False) + monkeypatch.delenv("LNAME", raising=False) + monkeypatch.delenv("USERNAME", raising=False) + assert get_user() is None + + class TestNumberedDir: PREFIX = "fun-"