2010-10-12 16:59:04 +08:00
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
.. _`tmpdir handling`:
|
2012-10-12 20:52:36 +08:00
|
|
|
.. _tmpdir:
|
2010-11-06 06:37:25 +08:00
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Temporary directories and files
|
2010-10-12 16:59:04 +08:00
|
|
|
================================================
|
|
|
|
|
2015-07-16 07:03:58 +08:00
|
|
|
The 'tmpdir' fixture
|
|
|
|
--------------------
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2015-07-16 07:03:58 +08:00
|
|
|
You can use the ``tmpdir`` fixture which will
|
2010-10-12 16:59:04 +08:00
|
|
|
provide a temporary directory unique to the test invocation,
|
|
|
|
created in the `base temporary directory`_.
|
|
|
|
|
|
|
|
``tmpdir`` is a `py.path.local`_ object which offers ``os.path`` methods
|
|
|
|
and more. Here is an example test usage::
|
|
|
|
|
|
|
|
# content of test_tmpdir.py
|
|
|
|
import os
|
|
|
|
def test_create_file(tmpdir):
|
|
|
|
p = tmpdir.mkdir("sub").join("hello.txt")
|
|
|
|
p.write("content")
|
|
|
|
assert p.read() == "content"
|
2010-11-06 18:38:53 +08:00
|
|
|
assert len(tmpdir.listdir()) == 1
|
2010-10-12 16:59:04 +08:00
|
|
|
assert 0
|
|
|
|
|
2010-11-06 18:38:53 +08:00
|
|
|
Running this would result in a passed test except for the last
|
2010-10-12 16:59:04 +08:00
|
|
|
``assert 0`` line which we use to look at values::
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2010-10-12 16:59:04 +08:00
|
|
|
$ py.test test_tmpdir.py
|
2015-06-07 05:30:49 +08:00
|
|
|
======= test session starts ========
|
2016-03-18 05:13:41 +08:00
|
|
|
platform linux -- Python 3.4.0, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
|
2015-06-07 05:30:49 +08:00
|
|
|
rootdir: $REGENDOC_TMPDIR, inifile:
|
2012-10-07 19:06:17 +08:00
|
|
|
collected 1 items
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
test_tmpdir.py F
|
|
|
|
|
2015-06-07 05:30:49 +08:00
|
|
|
======= FAILURES ========
|
|
|
|
_______ test_create_file ________
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2015-09-22 20:02:11 +08:00
|
|
|
tmpdir = local('PYTEST_TMPDIR/test_create_file0')
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
def test_create_file(tmpdir):
|
|
|
|
p = tmpdir.mkdir("sub").join("hello.txt")
|
|
|
|
p.write("content")
|
|
|
|
assert p.read() == "content"
|
2010-11-06 18:38:53 +08:00
|
|
|
assert len(tmpdir.listdir()) == 1
|
|
|
|
> assert 0
|
|
|
|
E assert 0
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
test_tmpdir.py:7: AssertionError
|
2015-06-07 05:30:49 +08:00
|
|
|
======= 1 failed in 0.12 seconds ========
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2015-07-16 07:03:58 +08:00
|
|
|
The 'tmpdir_factory' fixture
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
.. versionadded:: 2.8
|
|
|
|
|
|
|
|
The ``tmpdir_factory`` is a session-scoped fixture which can be used
|
|
|
|
to create arbitrary temporary directories from any other fixture or test.
|
|
|
|
|
|
|
|
For example, suppose your test suite needs a large image on disk, which is
|
|
|
|
generated procedurally. Instead of computing the same image for each test
|
|
|
|
that uses it into its own ``tmpdir``, you can generate it once per-session
|
|
|
|
to save time:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# contents of conftest.py
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def image_file(tmpdir_factory):
|
|
|
|
img = compute_expensive_image()
|
|
|
|
fn = tmpdir_factory.mktemp('data').join('img.png')
|
|
|
|
img.save(str(fn))
|
|
|
|
return fn
|
|
|
|
|
|
|
|
# contents of test_image.py
|
|
|
|
def test_histogram(image_file):
|
|
|
|
img = load_image(image_file)
|
|
|
|
# compute and test histogram
|
|
|
|
|
|
|
|
``tmpdir_factory`` instances have the following methods:
|
|
|
|
|
|
|
|
.. currentmodule:: _pytest.tmpdir
|
|
|
|
|
|
|
|
.. automethod:: TempdirFactory.mktemp
|
|
|
|
.. automethod:: TempdirFactory.getbasetemp
|
|
|
|
|
2010-10-12 16:59:04 +08:00
|
|
|
.. _`base temporary directory`:
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
The default base temporary directory
|
2010-10-12 16:59:04 +08:00
|
|
|
-----------------------------------------------
|
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
Temporary directories are by default created as sub-directories of
|
2010-11-22 00:43:18 +08:00
|
|
|
the system temporary directory. The base name will be ``pytest-NUM`` where
|
2011-02-17 21:46:40 +08:00
|
|
|
``NUM`` will be incremented with each test run. Moreover, entries older
|
2010-11-06 18:38:53 +08:00
|
|
|
than 3 temporary directories will be removed.
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2010-11-22 00:43:18 +08:00
|
|
|
You can override the default temporary directory setting like this::
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
py.test --basetemp=mydir
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
When distributing tests on the local machine, ``pytest`` takes care to
|
2010-12-24 04:56:38 +08:00
|
|
|
configure a basetemp directory for the sub processes such that all temporary
|
|
|
|
data lands below a single per-test run basetemp directory.
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2013-09-06 18:16:05 +08:00
|
|
|
.. _`py.path.local`: http://py.rtfd.org/en/latest/path.html
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
|