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
|
|
|
================================================
|
|
|
|
|
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
|
|
|
|
2019-04-28 23:37:58 +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`_.
|
|
|
|
|
2018-10-19 11:54:29 +08:00
|
|
|
``tmp_path`` is a ``pathlib/pathlib2.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
|
|
|
|
import os
|
2018-10-11 21:33:19 +08:00
|
|
|
|
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 ============================
|
2020-07-09 05:51:01 +08:00
|
|
|
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
|
2019-01-31 00:25:38 +08:00
|
|
|
cachedir: $PYTHON_PREFIX/.pytest_cache
|
2019-04-15 22:24:17 +08:00
|
|
|
rootdir: $REGENDOC_TMPDIR
|
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
|
|
|
|
|
|
|
|
test_tmp_path.py:13: 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
|
|
|
|
2019-04-28 23:37:58 +08:00
|
|
|
|
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.
|
|
|
|
|
2018-10-19 20:33:51 +08:00
|
|
|
It is intended to replace ``tmpdir_factory``, and returns :class:`pathlib.Path` instances.
|
2018-10-11 16:33:59 +08:00
|
|
|
|
2019-03-02 04:09:07 +08:00
|
|
|
See :ref:`tmp_path_factory API <tmp_path_factory factory api>` for details.
|
|
|
|
|
2018-10-11 16:33:59 +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
|
2019-08-07 07:20:06 +08:00
|
|
|
and more. Here is an example test usage:
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
2010-10-12 16:59:04 +08:00
|
|
|
# content of test_tmpdir.py
|
|
|
|
import os
|
2019-08-07 04:34:58 +08:00
|
|
|
|
|
|
|
|
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
|
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
|
2018-11-24 13:41:22 +08:00
|
|
|
``assert 0`` line which we use to look at values:
|
|
|
|
|
|
|
|
.. code-block:: pytest
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
$ pytest test_tmpdir.py
|
2017-11-23 23:33:41 +08:00
|
|
|
=========================== test session starts ============================
|
2020-07-09 05:51:01 +08:00
|
|
|
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
|
2019-01-31 00:25:38 +08:00
|
|
|
cachedir: $PYTHON_PREFIX/.pytest_cache
|
2019-04-15 22:24:17 +08:00
|
|
|
rootdir: $REGENDOC_TMPDIR
|
2017-07-04 07:29:13 +08:00
|
|
|
collected 1 item
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2017-11-23 23:33:41 +08:00
|
|
|
test_tmpdir.py F [100%]
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2017-11-23 23:33:41 +08:00
|
|
|
================================= FAILURES =================================
|
|
|
|
_____________________________ test_create_file _____________________________
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2015-09-22 20:02:11 +08:00
|
|
|
tmpdir = local('PYTEST_TMPDIR/test_create_file0')
|
2018-05-18 16:19:46 +08:00
|
|
|
|
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
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2019-08-16 08:00:09 +08:00
|
|
|
test_tmpdir.py:9: AssertionError
|
2020-03-11 22:23:25 +08:00
|
|
|
========================= short test summary info ==========================
|
|
|
|
FAILED test_tmpdir.py::test_create_file - assert 0
|
2019-08-30 23:43:47 +08:00
|
|
|
============================ 1 failed in 0.12s =============================
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2018-02-28 05:26:40 +08:00
|
|
|
.. _`tmpdir factory example`:
|
|
|
|
|
2015-07-16 07:03:58 +08:00
|
|
|
The 'tmpdir_factory' fixture
|
|
|
|
----------------------------
|
|
|
|
|
2019-04-28 23:37:58 +08:00
|
|
|
|
2015-07-16 07:03:58 +08:00
|
|
|
|
|
|
|
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")
|
2015-07-16 07:03:58 +08:00
|
|
|
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")
|
2015-07-16 07:03:58 +08:00
|
|
|
img.save(str(fn))
|
|
|
|
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
|
|
|
|
|
2018-02-28 05:26:40 +08:00
|
|
|
See :ref:`tmpdir_factory API <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
|
|
|
|
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
|
|
|
|
2018-01-07 00:01:34 +08:00
|
|
|
.. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html
|