Skip to content

Commit ba7bd3d

Browse files
committed
doc/tmpdir: significantly reduce space dedicated to tmpdir/tmpdir_factory
Emphasize tmp_path/tmp_path_factory and just note the legacy ones.
1 parent ef1308c commit ba7bd3d

File tree

2 files changed

+22
-84
lines changed

2 files changed

+22
-84
lines changed

doc/en/how-to/tmpdir.rst

Lines changed: 19 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ How to use temporary directories and files in tests
88
The ``tmp_path`` fixture
99
------------------------
1010

11-
12-
13-
1411
You can use the ``tmp_path`` fixture which will
1512
provide a temporary directory unique to the test invocation,
1613
created in the `base temporary directory`_.
1714

18-
``tmp_path`` is a ``pathlib.Path`` object. Here is an example test usage:
15+
``tmp_path`` is a :class:`pathlib.Path` object. Here is an example test usage:
1916

2017
.. code-block:: python
2118
@@ -71,82 +68,12 @@ Running this would result in a passed test except for the last
7168
The ``tmp_path_factory`` fixture
7269
--------------------------------
7370

74-
75-
76-
7771
The ``tmp_path_factory`` is a session-scoped fixture which can be used
7872
to create arbitrary temporary directories from any other fixture or test.
7973

80-
It is intended to replace ``tmpdir_factory``, and returns :class:`pathlib.Path` instances.
81-
82-
See :ref:`tmp_path_factory API <tmp_path_factory factory api>` for details.
83-
84-
85-
The 'tmpdir' fixture
86-
--------------------
87-
88-
You can use the ``tmpdir`` fixture which will
89-
provide a temporary directory unique to the test invocation,
90-
created in the `base temporary directory`_.
91-
92-
``tmpdir`` is a `py.path.local`_ object which offers ``os.path`` methods
93-
and more. Here is an example test usage:
94-
95-
.. code-block:: python
96-
97-
# content of test_tmpdir.py
98-
def test_create_file(tmpdir):
99-
p = tmpdir.mkdir("sub").join("hello.txt")
100-
p.write("content")
101-
assert p.read() == "content"
102-
assert len(tmpdir.listdir()) == 1
103-
assert 0
104-
105-
Running this would result in a passed test except for the last
106-
``assert 0`` line which we use to look at values:
107-
108-
.. code-block:: pytest
109-
110-
$ pytest test_tmpdir.py
111-
=========================== test session starts ============================
112-
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
113-
cachedir: $PYTHON_PREFIX/.pytest_cache
114-
rootdir: $REGENDOC_TMPDIR
115-
collected 1 item
116-
117-
test_tmpdir.py F [100%]
118-
119-
================================= FAILURES =================================
120-
_____________________________ test_create_file _____________________________
121-
122-
tmpdir = local('PYTEST_TMPDIR/test_create_file0')
123-
124-
def test_create_file(tmpdir):
125-
p = tmpdir.mkdir("sub").join("hello.txt")
126-
p.write("content")
127-
assert p.read() == "content"
128-
assert len(tmpdir.listdir()) == 1
129-
> assert 0
130-
E assert 0
131-
132-
test_tmpdir.py:6: AssertionError
133-
========================= short test summary info ==========================
134-
FAILED test_tmpdir.py::test_create_file - assert 0
135-
============================ 1 failed in 0.12s =============================
136-
137-
.. _`tmpdir factory example`:
138-
139-
The 'tmpdir_factory' fixture
140-
----------------------------
141-
142-
143-
144-
The ``tmpdir_factory`` is a session-scoped fixture which can be used
145-
to create arbitrary temporary directories from any other fixture or test.
146-
14774
For example, suppose your test suite needs a large image on disk, which is
14875
generated procedurally. Instead of computing the same image for each test
149-
that uses it into its own ``tmpdir``, you can generate it once per-session
76+
that uses it into its own ``tmp_path``, you can generate it once per-session
15077
to save time:
15178

15279
.. code-block:: python
@@ -156,10 +83,10 @@ to save time:
15683
15784
15885
@pytest.fixture(scope="session")
159-
def image_file(tmpdir_factory):
86+
def image_file(tmp_path_factory):
16087
img = compute_expensive_image()
161-
fn = tmpdir_factory.mktemp("data").join("img.png")
162-
img.save(str(fn))
88+
fn = tmp_path_factory.mktemp("data") / "img.png"
89+
img.save(fn)
16390
return fn
16491
16592
@@ -168,7 +95,20 @@ to save time:
16895
img = load_image(image_file)
16996
# compute and test histogram
17097
171-
See :ref:`tmpdir_factory API <tmpdir factory api>` for details.
98+
See :ref:`tmp_path_factory API <tmp_path_factory factory api>` for details.
99+
100+
.. _`tmpdir and tmpdir_factory`:
101+
102+
The ``tmpdir`` and ``tmpdir_factory`` fixtures
103+
---------------------------------------------------
104+
105+
The ``tmpdir`` and ``tmpdir_factory`` fixtures are similar to ``tmp_path``
106+
and ``tmp_path_factory``, but use/return legacy `py.path.local`_ objects
107+
rather than standard :class:`pathlib.Path` objects. These days, prefer to
108+
use ``tmp_path`` and ``tmp_path_factory``.
109+
110+
See :fixture:`tmpdir <tmpdir>` :fixture:`tmpdir_factory <tmpdir_factory>`
111+
API for details.
172112

173113

174114
.. _`base temporary directory`:

doc/en/reference/reference.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ tmp_path
582582
:no-auto-options:
583583

584584

585-
.. fixture:: _pytest.tmpdir.tmp_path_factory
585+
.. fixture:: tmp_path_factory
586586

587587
tmp_path_factory
588588
~~~~~~~~~~~~~~~~
@@ -601,7 +601,7 @@ tmp_path_factory
601601
tmpdir
602602
~~~~~~
603603

604-
:ref:`tmpdir`
604+
:ref:`tmpdir and tmpdir_factory`
605605

606606
.. autofunction:: _pytest.tmpdir.tmpdir()
607607
:no-auto-options:
@@ -612,9 +612,7 @@ tmpdir
612612
tmpdir_factory
613613
~~~~~~~~~~~~~~
614614

615-
:ref:`tmpdir factory example`
616-
617-
.. _`tmpdir factory api`:
615+
:ref:`tmpdir and tmpdir_factory`
618616

619617
``tmp_path_factory`` is an instance of :class:`~pytest.TempdirFactory`:
620618

0 commit comments

Comments
 (0)