Skip to content

Commit 87b740d

Browse files
committed
Add troubleshooting chapter for tmp_path caveat
- see #1143
1 parent 0d5d2b1 commit 87b740d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

docs/troubleshooting.rst

+24
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,30 @@ passed before the ``mocker`` fixture to ensure this:
266266
# works correctly
267267
mocker.patch("builtins.open", mocker.mock_open(read_data="content"))
268268
269+
tmp_path fixture with pyfakefs
270+
------------------------------
271+
If you are using the ``tmp_path`` fixture, or a similar pytest fixture
272+
relying on the real filesystem, you may have the opposite problem: now the ``fs``
273+
fixture must be added *after* the ``tmp_path`` fixture. Otherwise, the path will be
274+
created in the fake filesystem, and pytest will later try to access it in the real
275+
filesystem.
276+
277+
.. code:: python
278+
279+
def test_working(tmp_path, fs):
280+
fs.create_file(tmp_path / "foo")
281+
282+
283+
def test_not_working(fs, tmp_path):
284+
# causes an error while pytest tries to access the temporary directory
285+
pass
286+
287+
Note though that ``tmp_path`` and similar fixtures may not make much sense in the
288+
first place if used with ``pyfakefs``. While the directory will be created and
289+
removed in the real filesystem, all files you create will live in the fake filesystem
290+
only. You could as well use hardcoded paths in your tests, as they will not interfere
291+
with paths created in other tests.
292+
269293
Pathlib.Path objects created outside of tests
270294
---------------------------------------------
271295
A pattern which is seen more often with the increased usage of ``pathlib`` is the

0 commit comments

Comments
 (0)