diff --git a/_pytest/tmpdir.py b/_pytest/tmpdir.py index 44e980e2e..cff39065f 100644 --- a/_pytest/tmpdir.py +++ b/_pytest/tmpdir.py @@ -56,7 +56,11 @@ class TempdirFactory: # make_numbered_dir() call import getpass temproot = py.path.local.get_temproot() - rootdir = temproot.join('pytest-of-%s' % getpass.getuser()) + try: + rootdir = temproot.join('pytest-of-%s' % getpass.getuser()) + except ImportError: + # see issue #1010 + rootdir = temproot.join('pytest-tox') rootdir.ensure(dir=1) basetemp = py.path.local.make_numbered_dir(prefix='pytest-', rootdir=rootdir) diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 05b24fc59..0ea8e5263 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -117,4 +117,19 @@ def test_tmpdir_factory(testdir): session_dir.isdir() """) reprec = testdir.inline_run() - reprec.assertoutcome(passed=1) \ No newline at end of file + reprec.assertoutcome(passed=1) + + +def test_tmpdir_fallback_tox_env(testdir, monkeypatch): + """Test that tmpdir works even if environment variables required by getpass + module are missing (#1010). + """ + monkeypatch.delenv('USER', raising=False) + monkeypatch.delenv('USERNAME', raising=False) + testdir.makepyfile(""" + import pytest + def test_some(tmpdir): + assert tmpdir.isdir() + """) + reprec = testdir.inline_run() + reprec.assertoutcome(passed=1)