Skip to content

Commit ff786ff

Browse files
committed
Add documentation for problems with pyfakefs and mock_open
- see #631
1 parent c403e3d commit ff786ff

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

docs/intro.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ Limitations
8585
considered as reference systems, additionally the tests are run in Docker
8686
containers with the latest CentOS, Debian, Fedora and Ubuntu images.
8787

88+
- pyfakefs may not work correctly if file system functions are patched by
89+
other means (e.g. using `unittest.mock.patch`) - see
90+
:ref:`usage_with_mock_open` for more information
91+
8892
History
8993
-------
9094
pyfakefs was initially developed at Google by

docs/usage.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,3 +908,19 @@ that using ``fake_filesystem.set_uid()`` in your setup. This allows to run
908908
tests as non-root user in a root user environment and vice verse.
909909
Another possibility to run tests as non-root user in a root user environment
910910
is the convenience argument :ref:`allow_root_user`.
911+
912+
.. _usage_with_mock_open:
913+
914+
Pyfakefs and mock_open
915+
~~~~~~~~~~~~~~~~~~~~~~
916+
If you patch ``open`` using ``mock_open`` before the initialization of
917+
``pyfakefs``, it will not work properly, because the ``pyfakefs``
918+
initialization relies on ``open`` working correctly.
919+
Generally, you should not need ``mock_open`` if using ``pyfakefs``, because you
920+
always can create the files with the needed content using ``create_file``.
921+
This is true for patching any filesystem functions - avoid patching them
922+
while working with ``pyfakefs``.
923+
If you still want to use ``mock_open``, make sure it is only used while
924+
patching is in progress. For example, if you are using ``pytest`` with the
925+
``mocker`` fixture used to patch ``open``, make sure that the ``fs`` fixture is
926+
passed before the ``mocker`` fixture to ensure this.

0 commit comments

Comments
 (0)