diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 9c965444f04b23..f7408a83759789 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -2206,8 +2206,11 @@ def test_listdir(self): # test listdir without arguments current_directory = os.getcwd() try: - os.chdir(os.sep) - self.assertEqual(set(os.listdir()), set(os.listdir(os.sep))) + with tempfile.TemporaryDirectory() as tmpdir: + os.chdir(tmpdir) + open("a.txt", "w").close() + open("test_file.foo", "w").close() + self.assertEqual(set(os.listdir()), set(os.listdir(tmpdir))) finally: os.chdir(current_directory) diff --git a/Misc/NEWS.d/next/Tests/2020-03-16-19-38-15.bpo-39986.xK3wOi.rst b/Misc/NEWS.d/next/Tests/2020-03-16-19-38-15.bpo-39986.xK3wOi.rst new file mode 100644 index 00000000000000..cdf04faae80f48 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2020-03-16-19-38-15.bpo-39986.xK3wOi.rst @@ -0,0 +1,2 @@ +Make test_os test_listdir test robust against root directory changing while +the test runs.