Pytest: Should tmpdir clean up after itself?

Created on 21 Jul 2014  路  6Comments  路  Source: pytest-dev/pytest

Originally reported by: Christian Theune (BitBucket: ctheune, GitHub: ctheune)


We noticed that tmpdir isn't cleaning up after itself.

Is there a reason why not? The docs don't mention it. Otherwise: I'd be happy to provide a patch to make tmpdir clean up after itself.


bug

Most helpful comment

Currently not, but it is easy to add a new ini option that tells pytest how many previous versions to keep around:

[pytest]
tmpdir_keep=0

Currently the default is 3.

All 6 comments

_Original comment by_ Jurko Gospodneti膰 (BitBucket: jurko, GitHub: jurko):


I think pytest already does that. It just keeps the temporary test data around for the last few (5?) test runs, but all the ones before that are removed automatically. I haven't checked the source code, but I think that's how pytest behaves for me - Windows 7 installation.

IMO, retaining the temporary test data for the last few test runs is extremely useful when you need to debug a specific test failure.

Just my 2 cents worth...

Best regards,
Jurko Gospodneti膰

_Original comment by_ Floris Bruynooghe (BitBucket: flub, GitHub: flub):


What @jurko says is correct AFAIK, only 5-7 versions are kept around and you get a simlink /tmp/pytest-<username> to the most recent one. So cleanup already happens just a little later then you might expect.

I get that some people would prefer to keep some old test run data around. However frequently I have big test data and would like to have it cleaned up (particularly if the tests all pass). Is there a way for one to force cleanup after a run?

Currently not, but it is easy to add a new ini option that tells pytest how many previous versions to keep around:

[pytest]
tmpdir_keep=0

Currently the default is 3.

my pytest version is 4.3.1

but not work

$ ls -l
total 0
drwxr-xr-x  6 scottming  staff   192B Mar 21 19:37 pytest-12
drwxr-xr-x  6 scottming  staff   192B Mar 21 19:37 pytest-13
drwxr-xr-x  6 scottming  staff   192B Mar 21 19:38 pytest-14
lrwxr-xr-x  1 scottming  staff    86B Mar 21 19:38 pytest-current -> /private/var/folders/tf/2yqh3xmj5sl7rb3pbsv_p_ww0000gp/T/pytest-of-scottming/pytest-14

this is the content in my pytest.ini file:

[pytest]
tmpdir_keep=0

this is the test code:

CONTENT = "content"


def test_create_file(tmp_path):
    print('tmp_path"', tmp_path)

    d = tmp_path / "sub"
    d.mkdir()
    p = d / "hello.txt"
    p.write_text(CONTENT)
    assert p.read_text() == CONTENT
    assert len(list(tmp_path.iterdir())) == 1
    assert 0

Hi @scottming,

The tmpdir_keep option does not exist, it was merely a suggestion in https://github.com/pytest-dev/pytest/issues/543#issuecomment-306644153 as something that could be implemented. 馃槈

Was this page helpful?
0 / 5 - 0 ratings