Python: How do I make temporary files in my test suite? Python: How do I make temporary files in my test suite? python python

Python: How do I make temporary files in my test suite?


FWIW using py.test you can write:

def test_function(tmpdir):    # tmpdir is a unique-per-test-function invocation temporary directory

Each test function using the "tmpdir" function argument will get a clean empty directory, created as a sub directory of "/tmp/pytest-NUM" (linux, win32 has different path) where NUM is increased for each test run. The last three directories are kept to ease inspection and older ones are automatically deleted. You can also set the base temp directory with py.test --basetemp=mytmpdir.

The tmpdir object is a py.path.local object which can also use like this:

sub = tmpdir.mkdir("sub")sub.join("testfile.txt").write("content")

But it's also fine to just convert it to a "string" path:

tmpdir = str(tmpdir)


See the tempfile module in the standard library -- should be all you need.


Instead of using tempfile directly I suggest using a context manager wrapper for it - the context manager takes care of removing the directory in all cases (success/failure/exception) with basically no boilerplate.

Here is how it can be used:

from tempfile import TempDir    # "tempfile" is a module in the standard library...# in some test:with TempDir() as d:    temp_file_name = os.path.join(d.name, 'your_temp_file.name')    # create file...    # ...    # asserts...

I have been using a home grown version (the implementation is rather short - under 20 lines) up to the point, when I needed to use it somewhere else as well, so I looked around if there is a package ready to install, and indeed there is: tempfile


Note: the code snippet above is a little out-dated.