question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

flag to cleanup generated tmp_path objects

See original GitHub issue

When using the tmp_path factory, the path remains until:

  • The system is restarted
  • The number of tmp_path objects is greater than the limit (currently 3)
  • a basepath is given (This will just overwrite the existing basepath, but will not provide any cleanup).

I propose a flag that cleans up tmp_path objects on completion of the test.

Usage would look like

python -m pytest --autocleanup

This would execute the following steps:

  1. create a list of each tmp_path object created.
  2. execute code
  3. iterate through the list deleting each tmp_path that was created (if it still exists)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nicoddemuscommented, Jul 9, 2020

@asottile’s suggestion is on point, and in fact you might even overwrite the builtin fixture into your root conftest.py:

@pytest.fixture
def tmp_path(tmp_path):
    yield tmp_path
    shutil.rmtree(tmp_path)

I’m closing because I believe this should be enough to fix OP’s problem without changing anything in pytest.

@kjaymiller feel free to follow up with further questions though.

1reaction
RonnyPfannschmidtcommented, Jul 9, 2020

May we know the surrounding use case that makes the current auto cleanup misfit?

The folders intentionally stay around to do post mortem analysis

Read more comments on GitHub >

github_iconTop Results From Across the Web

tempfile — Generate temporary files and directories — Python ...
Return a file-like object that can be used as a temporary storage area. ... O_TMPFILE flag is used if it is available and...
Read more >
[testing] automatically clean up temp dirs during teardown #6471
Write a custom context manager that takes a hard-coded path and a flag to clean up or not to make it easy to...
Read more >
How do I automatically delete temp files in C#? - Stack Overflow
You could P/Invoke CreateFile and pass the FILE_FLAG_DELETE_ON_CLOSE flag. ... Delete(tempFileToDelete); } } } // Clean up using (File.
Read more >
How to use temporary directories and files in tests - Pytest
You can use the tmp_path fixture which will provide a temporary directory unique to the test invocation, created in the base temporary directory....
Read more >
ansible-runner Documentation - Read the Docs
Ansible Runner is a tool and python library that helps when interfacing with Ansible directly or as part of another.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found