2010-10-12 16:59:04 +08:00
|
|
|
|
2021-07-14 23:53:27 +08:00
|
|
|
.. _`tmp_path handling`:
|
|
|
|
.. _tmp_path:
|
2010-11-06 06:37:25 +08:00
|
|
|
|
2021-03-11 03:18:21 +08:00
|
|
|
How to use temporary directories and files in tests
|
|
|
|
===================================================
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2018-10-11 16:33:59 +08:00
|
|
|
The ``tmp_path`` fixture
|
2018-10-11 21:33:19 +08:00
|
|
|
------------------------
|
2018-10-11 16:33:59 +08:00
|
|
|
|
2018-10-19 11:54:29 +08:00
|
|
|
You can use the ``tmp_path`` fixture which will
|
2018-10-11 16:33:59 +08:00
|
|
|
provide a temporary directory unique to the test invocation,
|
|
|
|
created in the `base temporary directory`_.
|
|
|
|
|
2021-03-14 01:45:11 +08:00
|
|
|
``tmp_path`` is a :class:`pathlib.Path` object. Here is an example test usage:
|
2018-10-11 21:33:19 +08:00
|
|
|
|
|
|
|
.. code-block:: python
|
2018-10-11 16:33:59 +08:00
|
|
|
|
|
|
|
# content of test_tmp_path.py
|
2019-06-05 08:48:06 +08:00
|
|
|
CONTENT = "content"
|
2018-10-11 16:33:59 +08:00
|
|
|
|
2018-10-11 21:33:19 +08:00
|
|
|
|
2018-10-11 16:33:59 +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
|
2018-10-11 16:33:59 +08:00
|
|
|
assert 0
|
|
|
|
|
|
|
|
Running this would result in a passed test except for the last
|
2018-11-24 13:41:22 +08:00
|
|
|
``assert 0`` line which we use to look at values:
|
|
|
|
|
|
|
|
.. code-block:: pytest
|
2018-10-11 16:33:59 +08:00
|
|
|
|
|
|
|
$ pytest test_tmp_path.py
|
2018-10-16 04:23:30 +08:00
|
|
|
=========================== test session starts ============================
|
2021-08-31 01:40:59 +08:00
|
|
|
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-1.x.y
|
2021-10-04 14:56:26 +08:00
|
|
|
cachedir: .pytest_cache
|
|
|
|
rootdir: /home/sweet/project
|
2018-10-16 04:23:30 +08:00
|
|
|
collected 1 item
|
|
|
|
|
|
|
|
test_tmp_path.py F [100%]
|
|
|
|
|
|
|
|
================================= FAILURES =================================
|
|
|
|
_____________________________ test_create_file _____________________________
|
|
|
|
|
|
|
|
tmp_path = PosixPath('PYTEST_TMPDIR/test_create_file0')
|
|
|
|
|
|
|
|
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
|
|
|
|
assert len(list(tmp_path.iterdir())) == 1
|
|
|
|
> assert 0
|
|
|
|
E assert 0
|
|
|
|
|
2020-12-13 05:21:28 +08:00
|
|
|
test_tmp_path.py:11: AssertionError
|
2020-03-11 22:23:25 +08:00
|
|
|
========================= short test summary info ==========================
|
|
|
|
FAILED test_tmp_path.py::test_create_file - assert 0
|
2019-08-30 23:43:47 +08:00
|
|
|
============================ 1 failed in 0.12s =============================
|
2018-10-11 16:33:59 +08:00
|
|
|
|
2019-03-02 04:09:07 +08:00
|
|
|
.. _`tmp_path_factory example`:
|
|
|
|
|
2018-10-11 21:33:19 +08:00
|
|
|
The ``tmp_path_factory`` fixture
|
|
|
|
--------------------------------
|
2018-10-11 16:33:59 +08:00
|
|
|
|
2018-10-20 01:22:04 +08:00
|
|
|
The ``tmp_path_factory`` is a session-scoped fixture which can be used
|
2018-10-11 16:33:59 +08:00
|
|
|
to create arbitrary temporary directories from any other fixture or test.
|
|
|
|
|
2015-07-16 07:03:58 +08:00
|
|
|
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
|
2021-03-14 01:45:11 +08:00
|
|
|
that uses it into its own ``tmp_path``, you can generate it once per-session
|
2015-07-16 07:03:58 +08:00
|
|
|
to save time:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# contents of conftest.py
|
|
|
|
import pytest
|
|
|
|
|
2018-06-03 11:29:28 +08:00
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
2021-03-14 01:45:11 +08:00
|
|
|
def image_file(tmp_path_factory):
|
2015-07-16 07:03:58 +08:00
|
|
|
img = compute_expensive_image()
|
2021-03-14 01:45:11 +08:00
|
|
|
fn = tmp_path_factory.mktemp("data") / "img.png"
|
|
|
|
img.save(fn)
|
2015-07-16 07:03:58 +08:00
|
|
|
return fn
|
|
|
|
|
2018-06-03 11:29:28 +08:00
|
|
|
|
2015-07-16 07:03:58 +08:00
|
|
|
# contents of test_image.py
|
|
|
|
def test_histogram(image_file):
|
|
|
|
img = load_image(image_file)
|
|
|
|
# compute and test histogram
|
|
|
|
|
2021-03-14 01:45:11 +08:00
|
|
|
See :ref:`tmp_path_factory API <tmp_path_factory factory api>` for details.
|
|
|
|
|
|
|
|
.. _`tmpdir and tmpdir_factory`:
|
2021-07-14 23:53:27 +08:00
|
|
|
.. _tmpdir:
|
2021-03-14 01:45:11 +08:00
|
|
|
|
|
|
|
The ``tmpdir`` and ``tmpdir_factory`` fixtures
|
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
The ``tmpdir`` and ``tmpdir_factory`` fixtures are similar to ``tmp_path``
|
|
|
|
and ``tmp_path_factory``, but use/return legacy `py.path.local`_ objects
|
|
|
|
rather than standard :class:`pathlib.Path` objects. These days, prefer to
|
|
|
|
use ``tmp_path`` and ``tmp_path_factory``.
|
|
|
|
|
|
|
|
See :fixture:`tmpdir <tmpdir>` :fixture:`tmpdir_factory <tmpdir_factory>`
|
|
|
|
API for details.
|
2015-07-16 07:03:58 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-02-15 21:10:37 +08:00
|
|
|
You can override the default temporary directory setting like this:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest --basetemp=mydir
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2020-07-29 22:58:18 +08:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
The contents of ``mydir`` will be completely removed, so make sure to use a directory
|
|
|
|
for that purpose only.
|
|
|
|
|
|
|
|
When distributing tests on the local machine using ``pytest-xdist``, care is taken to
|
|
|
|
automatically configure a basetemp directory for the sub processes such that all temporary
|
2010-12-24 04:56:38 +08:00
|
|
|
data lands below a single per-test run basetemp directory.
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2018-01-07 00:01:34 +08:00
|
|
|
.. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html
|