test_ok2/doc/en/tmpdir.rst

159 lines
4.5 KiB
ReStructuredText
Raw Normal View History

2010-11-06 06:37:25 +08:00
.. _`tmpdir handling`:
.. _tmpdir:
2010-11-06 06:37:25 +08:00
Temporary directories and files
================================================
The ``tmp_path`` fixture
2018-10-11 21:33:19 +08:00
------------------------
.. versionadded:: 3.9
You can use the ``tmpdir`` fixture which will
provide a temporary directory unique to the test invocation,
created in the `base temporary directory`_.
2018-10-11 21:33:19 +08:00
``tmpdir`` is a ``pathlib/pathlib2.Path`` object. Here is an example test usage:
.. code-block:: python
# content of test_tmp_path.py
import os
2018-10-11 21:33:19 +08:00
CONTENT = u"content"
2018-10-11 21:33:19 +08:00
def test_create_file(tmp_path):
d = tmp_path / "sub"
d.mkdir()
p = d / "hello.txt"
p.write_text(CONTENT)
assert p.read_text() == CONTENT
2018-10-16 04:17:02 +08:00
assert len(list(tmp_path.iterdir())) == 1
assert 0
Running this would result in a passed test except for the last
``assert 0`` line which we use to look at values::
$ pytest test_tmp_path.py
... #fill fom regendoc
2018-10-11 21:33:19 +08:00
The ``tmp_path_factory`` fixture
--------------------------------
.. versionadded:: 3.9
The ``tmp_path_facotry`` is a session-scoped fixture which can be used
to create arbitrary temporary directories from any other fixture or test.
2018-10-11 21:33:19 +08:00
its intended to replace ``tmpdir_factory`` and returns :class:`pathlib.Path` instances.
The 'tmpdir' fixture
--------------------
You can use the ``tmpdir`` fixture which will
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"
assert len(tmpdir.listdir()) == 1
assert 0
Running this would result in a passed test except for the last
``assert 0`` line which we use to look at values::
$ pytest test_tmpdir.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
2017-03-14 06:41:20 +08:00
rootdir: $REGENDOC_TMPDIR, inifile:
2017-07-04 07:29:13 +08:00
collected 1 item
test_tmpdir.py F [100%]
================================= FAILURES =================================
_____________________________ test_create_file _____________________________
tmpdir = local('PYTEST_TMPDIR/test_create_file0')
def test_create_file(tmpdir):
p = tmpdir.mkdir("sub").join("hello.txt")
p.write("content")
assert p.read() == "content"
assert len(tmpdir.listdir()) == 1
> assert 0
E assert 0
test_tmpdir.py:7: AssertionError
========================= 1 failed in 0.12 seconds =========================
.. _`tmpdir factory example`:
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
2018-06-03 11:29:28 +08:00
@pytest.fixture(scope="session")
def image_file(tmpdir_factory):
img = compute_expensive_image()
2018-06-03 11:29:28 +08:00
fn = tmpdir_factory.mktemp("data").join("img.png")
img.save(str(fn))
return fn
2018-06-03 11:29:28 +08:00
# contents of test_image.py
def test_histogram(image_file):
img = load_image(image_file)
# compute and test histogram
See :ref:`tmpdir_factory API <tmpdir factory api>` for details.
.. _`base temporary directory`:
The default base temporary directory
-----------------------------------------------
Temporary directories are by default created as sub-directories of
the system temporary directory. The base name will be ``pytest-NUM`` where
``NUM`` will be incremented with each test run. Moreover, entries older
than 3 temporary directories will be removed.
You can override the default temporary directory setting like this::
pytest --basetemp=mydir
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.
.. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html