[issue39029] TestMaildir.test_clean fails randomly under parallel tests

Karthikeyan Singaravelan report at bugs.python.org
Mon Dec 30 05:37:46 EST 2019


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

> Should not parallel tests be ran in different directories?

yes, the test uses support.TESTFN which has the value of "{}_{}_tmp".format(TESTFN, os.getpid()) in the setUp [0] to create tempdir. Under parallel tests the pid value of TESTFN is initialised with a pid once and used always in the setup calls. So setup acts under parallel tests with same test folder. The tempdir can be dynamically generated but I guess this pattern to use support.TESTFN is common and there could be other flaky tests.

# create tempdir at setup each time and clean it up.

diff --git Lib/test/test_mailbox.py Lib/test/test_mailbox.py
index 36a265390e..aa8ae9ae20 100644
--- Lib/test/test_mailbox.py
+++ Lib/test/test_mailbox.py
@@ -51,13 +51,15 @@ class TestMailbox(TestBase):
     _template = 'From: foo\n\n%s\n'

     def setUp(self):
-        self._path = support.TESTFN
+        self._tempdir = tempfile.TemporaryDirectory()
+        self._path = self._tempdir.name
         self._delete_recursively(self._path)
         self._box = self._factory(self._path)

     def tearDown(self):
         self._box.close()
         self._delete_recursively(self._path)
+        self._tempdir.cleanup()

     def test_add(self):
         # Add copies of a sample message

[0] https://github.com/python/cpython/blob/88dce26da6bc4838092128d9a6f1c98bf48b7c90/Lib/test/test_mailbox.py#L54

----------
nosy: +vstinner

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39029>
_______________________________________


More information about the Python-bugs-list mailing list