@@ -8,14 +8,11 @@ How to use temporary directories and files in tests
8
8
The ``tmp_path `` fixture
9
9
------------------------
10
10
11
-
12
-
13
-
14
11
You can use the ``tmp_path `` fixture which will
15
12
provide a temporary directory unique to the test invocation,
16
13
created in the `base temporary directory `_.
17
14
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:
19
16
20
17
.. code-block :: python
21
18
@@ -71,82 +68,12 @@ Running this would result in a passed test except for the last
71
68
The ``tmp_path_factory `` fixture
72
69
--------------------------------
73
70
74
-
75
-
76
-
77
71
The ``tmp_path_factory `` is a session-scoped fixture which can be used
78
72
to create arbitrary temporary directories from any other fixture or test.
79
73
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
-
147
74
For example, suppose your test suite needs a large image on disk, which is
148
75
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
150
77
to save time:
151
78
152
79
.. code-block :: python
@@ -156,10 +83,10 @@ to save time:
156
83
157
84
158
85
@pytest.fixture (scope = " session" )
159
- def image_file (tmpdir_factory ):
86
+ def image_file (tmp_path_factory ):
160
87
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 )
163
90
return fn
164
91
165
92
@@ -168,7 +95,20 @@ to save time:
168
95
img = load_image(image_file)
169
96
# compute and test histogram
170
97
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.
172
112
173
113
174
114
.. _`base temporary directory` :
0 commit comments