<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Feb 14, 2019 at 4:03 PM Tim Golden <<a href="mailto:mail@timgolden.me.uk">mail@timgolden.me.uk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 14/02/2019 14:56, Giampaolo Rodola' wrote:<br>
> <br>
> <br>
> On Thu, Feb 14, 2019 at 3:25 PM Eric Snow <<a href="mailto:ericsnowcurrently@gmail.com" target="_blank">ericsnowcurrently@gmail.com</a> <br>
> <mailto:<a href="mailto:ericsnowcurrently@gmail.com" target="_blank">ericsnowcurrently@gmail.com</a>>> wrote:<br>
> <br>
> On Thu, Feb 14, 2019, 02:47 Ronald Oussoren via Python-Dev<br>
> <<a href="mailto:python-dev@python.org" target="_blank">python-dev@python.org</a> <mailto:<a href="mailto:python-dev@python.org" target="_blank">python-dev@python.org</a>> wrote:<br>
> <br>
> <br>
> I usually use shutil.rmtree for tests that need to create<br>
> temporary files, and create a temporary directory for those<br>
> files (that is, use tempfile.mkdtemp in setUp() and use<br>
> shutil.rmtree in tearDown()). That way I don’t have to adjust<br>
> house-keeping code when I make changes to test code.<br>
> <br>
> <br>
> Same here.<br>
> <br>
> -eric<br>
> <br>
> <br>
> What I generally do is avoid relying on tempfile.mkdtemp() and always <br>
> use TESTFN instead. I think it's cleaner as a pradigm because it's an <br>
> incentive to not pollute the single unit tests with `self.addCleanup()` <br>
> instructions (the whole cleanup logic is always supposed to occur in <br>
> setUp/tearDown):<br>
<br>
Must chime in here because I've been pushing (variously months & years <br>
ago) to move *away* from TESTFN because it generates numerous <br>
intermittent errors on my Windows setup. I've had several goes at <br>
starting to do that but a combination of my own lack of time plus some <br>
people's reluctance to go that route altogether has stalled the thing.<br>
<br>
I'm not sure I understand the difference in cleanup/teardown terms <br>
between using tempfile and using TESTFN. The objections I've seen from <br>
people (apart, obviously, from test churn) are to do with building up <br>
testing temp artefacts on a possibly low-sized disk.<br>
<br>
TJG<br>
</blockquote></div><div><br></div><div>I suppose you mean the intermittent failures are usually due to "file is already in use by another process" correct? test.support's unlink(), rmdir() and rmtree() functions already implement a retry-with-timeout logic in order to prevent this issue. I suppose when this issue may still occur, though, is when the file/handle is held by another process, meaning that the unit-test probably forgot to terminate()/wait() a subprocess or should have used support.read_children(). In summary, my approach is more "strict" because it implies that unit-tests always do a proper cleanup. tempfile.mkdtemp() may prevent failures but it may hide a unit-test which doesn't do a proper file/dir cleanup and should have been fixed instead. The drawback in practical terms is that orphaned test files are left behind.</div><div><br></div><div>Extra: an argument in favor of using tempfile.mkdtemp() instead of TESTFN is parallel testing, but I think we're not using it.</div><div><br></div><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div>Giampaolo - <a href="http://grodola.blogspot.com" target="_blank">http://grodola.blogspot.com</a></div><div><br></div></div></div></div>