Lines Matching +full:pytest +full:- +full:test
4 Test Scenarios
5 --------------
8 Patch using the pytest plugin
10 ``pyfakefs`` functions as a `pytest`_ plugin that provides the `fs` fixture,
16 Here is an example for a simple test:
27 (invalid-name)``,
33 @pytest.fixture
34 def fake_filesystem(fs): # pylint:disable=invalid-name
40 Class-, module- and session-scoped fixtures
42 For convenience, class-, module- and session-scoped fixtures with the same
49 done in the fake filesystem inside a test will remain there until the respective scope
55 use test classes derived from ``fake_filesystem_unittest.TestCase``.
71 file_path = "/test/file.txt"
92 pathlib.Path("/test/file1.txt").touch()
94 cls.fake_fs().create_file("/test/file2.txt", contents="test")
97 self.assertTrue(os.path.exists("/test/file1.txt"))
98 self.assertTrue(os.path.exists("/test/file2.txt"))
101 self.assertTrue(os.path.exists("/test/file1.txt"))
102 file_path = "/test/file3.txt"
110 .. caution:: If this is used, any changes made in the fake filesystem inside a test
111 will remain there for all following tests in the test class, if they are not reverted
112 in the test itself.
118 you can do the patching using ``fake_filesystem_unittest.Patcher``--the class
130 patcher.fs.create_file("/foo/bar", contents="test")
150 If you are not using ``pytest`` and want to use the fake filesystem for a
161 fake_fs.create_file("/foo/bar", contents="test")
193 You can also use this to make a single unit test use the fake fs:
200 fs.create_file("/foo/bar", contents="test")
206 --------------------
210 can also be used with ``unittest`` and ``pytest``.
229 Pytest subsection
232 In case of ``pytest``, you have two possibilities:
234 - The standard way to customize the ``fs`` fixture is to write your own
239 import pytest
243 @pytest.fixture
252 - You can also pass the arguments using ``@pytest.mark.parametrize``. Note that
256 single test:
260 import pytest
263 @pytest.mark.parametrize("fs", [[None, None, None, False]], indirect=True)
310 ``Pyfakefs`` patches modules that are imported before starting the test by
311 finding and replacing file system modules in all loaded modules at test
315 - imported directly, for example:
322 - imported as another name:
328 - imported using one of these two specially handled statements:
376 In this case, ``path`` will hold the real file system path inside the test.
390 To get these cases to work as expected under test, the respective modules
415 # example using pytest
416 @pytest.mark.parametrize("fs", [[None, [example.sut]]], indirect=True)
484 # test code using Patcher
488 # test code using unittest
497 # test code using pytest
498 @pytest.mark.parametrize(
505 # test code using patchfs decorator
515 the file system after test setup. To make this possible, the affected module
538 your tests as a non-root user regardless of the actual user rights, you may
548 ``read_excel``, and for ``Django`` file locks--more may follow. Ordinarily,
551 may be removed or replaced by more fine-grained arguments in future releases.
556 opens a file read-only and is used to open Python code files. By default, this
600 - if default arguments are *computed* using file system functions:
610 - if the default argument is an instance of ``pathlib.Path``:
626 If True (the default), patched and non-patched modules are cached between tests
628 patching itself is reverted after each test). This argument allows to turn it off in case it causes…
634 fake_fs.create_file("foo", contents="test")
637 If using ``pytest``, the cache is always cleared before the final test shutdown, as there has been …
639 This does not happen for other test methods so far.
654 If you want to clear the cache just for a specific test instead, you can call
659 def test_something(fs): # using pytest fixture
667 -------------------------
672 fixture in pytest, ``TestCase.fs`` for ``unittest``, and the ``fs`` argument
683 size without contents--in this case, you will not be able to perform
685 with large files, see also :ref:`set-fs-size`).
697 file_path = "/foo/bar/test.txt"
698 self.fs.create_file(file_path, contents="test")
700 self.assertEqual("test", f.read())
722 by default read-only, but even if you add them using ``read_only=False``,
750 You can do the same using ``pytest`` by using a fixture for test setup:
754 import pytest
760 @pytest.fixture
766 @pytest.mark.usefixtures("my_fs")
772 If you are not using the fixture directly in the test, you can use
773 ``@pytest.mark.usefixtures`` instead of passing the fixture as an argument.
776 When using ``pytest`` another option is to load the contents of the real file
777 in a fixture and pass this fixture to the test function **before** passing
782 import pytest
786 @pytest.fixture
815 .. _set-fs-size:
851 Sometimes, you may want to access the real filesystem inside the test with
858 Here is an example that tests the usage with the ``pyfakefs`` pytest fixture:
907 - ``is_windows_fs`` - if ``True`` a Windows file system (NTFS) is assumed
908 - ``is_macos`` - if ``True`` and ``is_windows_fs`` is ``False``, the
910 - if ``is_windows_fs`` and ``is_macos`` are ``False``, a Linux file system
912 - ``is_case_sensitive`` is set to ``True`` under Linux and to ``False``
913 under Windows and macOS by default - you can change it to change the
915 - ``path_separator`` is set to ``\`` under Windows and to ``/`` under Posix,
917 ``None`` under Posix--these can also be adapted if needed
919 The following test works both under Windows and Linux:
938 supported in the Python filesystem API. To make this possible to test, there is the
952 with pytest.raises(PermissionError):
956 .. _`example.py`: https://github.com/pytest-dev/pyfakefs/blob/main/pyfakefs/tests/example.py
957 .. _`example_test.py`: https://github.com/pytest-dev/pyfakefs/blob/main/pyfakefs/tests/example_test…
958 .. _`pytest`: https://doc.pytest.org target
960 .. _`all Patcher arguments`: https://pytest-pyfakefs.readthedocs.io/en/latest/modules.html#pyfakefs…