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
|
|
|
|
2024-01-18 18:21:49 +08:00
|
|
|
You can use the ``tmp_path`` fixture which will provide a temporary directory
|
|
|
|
unique to each test function.
|
2018-10-11 16:33:59 +08:00
|
|
|
|
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"
|
2023-07-09 02:40:05 +08:00
|
|
|
p.write_text(CONTENT, encoding="utf-8")
|
|
|
|
assert p.read_text(encoding="utf-8") == 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 ============================
|
2024-01-02 16:58:20 +08:00
|
|
|
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
|
2021-10-04 14:56:26 +08:00
|
|
|
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"
|
2023-09-02 23:41:32 +08:00
|
|
|
p.write_text(CONTENT, encoding="utf-8")
|
|
|
|
assert p.read_text(encoding="utf-8") == CONTENT
|
2018-10-16 04:23:30 +08:00
|
|
|
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
|
|
|
|
2024-01-18 18:21:49 +08:00
|
|
|
By default, ``pytest`` retains the temporary directory for the last 3 ``pytest``
|
|
|
|
invocations. Concurrent invocations of the same test function are supported by
|
|
|
|
configuring the base temporary directory to be unique for each concurrent
|
|
|
|
run. See `temporary directory location and retention`_ for details.
|
|
|
|
|
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
|
2024-01-18 18:21:49 +08:00
|
|
|
----------------------------------------------
|
2021-03-14 01:45:11 +08:00
|
|
|
|
|
|
|
The ``tmpdir`` and ``tmpdir_factory`` fixtures are similar to ``tmp_path``
|
|
|
|
and ``tmp_path_factory``, but use/return legacy `py.path.local`_ objects
|
2022-07-15 19:40:18 +08:00
|
|
|
rather than standard :class:`pathlib.Path` objects.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
These days, it is preferred to use ``tmp_path`` and ``tmp_path_factory``.
|
2021-03-14 01:45:11 +08:00
|
|
|
|
2022-10-01 17:44:06 +08:00
|
|
|
In order to help modernize old code bases, one can run pytest with the legacypath
|
|
|
|
plugin disabled:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
pytest -p no:legacypath
|
|
|
|
|
|
|
|
This will trigger errors on tests using the legacy paths.
|
|
|
|
It can also be permanently set as part of the :confval:`addopts` parameter in the
|
|
|
|
config file.
|
|
|
|
|
2021-03-14 01:45:11 +08:00
|
|
|
See :fixture:`tmpdir <tmpdir>` :fixture:`tmpdir_factory <tmpdir_factory>`
|
|
|
|
API for details.
|
2015-07-16 07:03:58 +08:00
|
|
|
|
|
|
|
|
2024-01-18 18:21:49 +08:00
|
|
|
.. _`temporary directory location and retention`:
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2024-01-18 18:21:49 +08:00
|
|
|
Temporary directory location and retention
|
|
|
|
------------------------------------------
|
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
|
2022-12-01 21:29:46 +08:00
|
|
|
``NUM`` will be incremented with each test run.
|
2022-12-24 23:18:38 +08:00
|
|
|
By default, entries older than 3 temporary directories will be removed.
|
2022-12-01 21:29:46 +08:00
|
|
|
This behavior can be configured with :confval:`tmp_path_retention_count` and
|
|
|
|
:confval:`tmp_path_retention_policy`.
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2022-12-01 21:29:46 +08:00
|
|
|
Using the ``--basetemp``
|
2022-02-04 20:38:26 +08:00
|
|
|
option will remove the directory before every run, effectively meaning the temporary directories
|
|
|
|
of only the most recent run will be kept.
|
|
|
|
|
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
|
2024-01-18 18:21:49 +08:00
|
|
|
automatically configure a `basetemp` directory for the sub processes such that all temporary
|
|
|
|
data lands below a single per-test run temporary 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
|