aboutsummaryrefslogtreecommitdiff
path: root/docs/autopatch.rst
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-09-21 21:59:40 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-09-21 21:59:40 +0000
commitc7efe3dcf86b7035b6942cbdfad6c8479a5ad7b0 (patch)
treef40c1a21c47e1a74164c5feb8a577145a2970aeb /docs/autopatch.rst
parentf9490653f18ce1f2997b79d6ef6d46f2b10ee44d (diff)
parentee132a3ba2a7295008913e19af9328d0954f240e (diff)
downloadpyfakefs-android14-qpr2-s3-release.tar.gz
Change-Id: I60abe1d180cfce33615c248c2f4f7691274bd387
Diffstat (limited to 'docs/autopatch.rst')
-rw-r--r--docs/autopatch.rst29
1 files changed, 17 insertions, 12 deletions
diff --git a/docs/autopatch.rst b/docs/autopatch.rst
index 66d6a22..c317964 100644
--- a/docs/autopatch.rst
+++ b/docs/autopatch.rst
@@ -11,6 +11,11 @@ The pyfakefs source code contains files that demonstrate this usage model:
- ``example_test.py`` tests ``example.py``. During testing, the pyfakefs fake
file system is used by ``example_test.py`` and ``example.py`` alike.
+.. note:: This example uses the Python ``unittest`` module for testing, but the
+ functionality is similar if using the ``fs`` fixture in ``pytest``,
+ the ``patchfs`` decorator, or the ``Patcher`` class.
+
+
Software Under Test
-------------------
``example.py`` contains a few functions that manipulate files. For instance:
@@ -18,7 +23,7 @@ Software Under Test
.. code:: python
def create_file(path):
- '''Create the specified file and add some content to it. Use the open()
+ """Create the specified file and add some content to it. Use the open()
built in function.
For example, the following file operations occur in the fake file system.
@@ -37,8 +42,8 @@ Software Under Test
>>> with open('/test/file.txt') as f:
... f.readlines()
["This is test file '/test/file.txt'.\\n", 'It was created using the open() function.\\n']
- '''
- with open(path, 'w') as f:
+ """
+ with open(path, "w") as f:
f.write("This is test file '{}'.\n".format(path))
f.write("It was created using the open() function.\n")
@@ -60,6 +65,7 @@ and modules:
import os
import unittest
from pyfakefs import fake_filesystem_unittest
+
# The module under test is example:
import example
@@ -71,7 +77,7 @@ Doctests
.. code:: python
def load_tests(loader, tests, ignore):
- '''Load the pyfakefs/example.py doctest tests into unittest.'''
+ """Load the pyfakefs/example.py doctest tests into unittest."""
return fake_filesystem_unittest.load_doctests(loader, tests, ignore, example)
@@ -89,7 +95,6 @@ Next comes the ``unittest`` test class. This class is derived from
.. code:: python
class TestExample(fake_filesystem_unittest.TestCase):
-
def setUp(self):
self.setUpPyfakefs()
@@ -98,16 +103,16 @@ Next comes the ``unittest`` test class. This class is derived from
pass
def test_create_file(self):
- '''Test example.create_file()'''
+ """Test example.create_file()"""
# The os module has been replaced with the fake os module so all of the
# following occurs in the fake filesystem.
- self.assertFalse(os.path.isdir('/test'))
- os.mkdir('/test')
- self.assertTrue(os.path.isdir('/test'))
+ self.assertFalse(os.path.isdir("/test"))
+ os.mkdir("/test")
+ self.assertTrue(os.path.isdir("/test"))
- self.assertFalse(os.path.exists('/test/file.txt'))
- example.create_file('/test/file.txt')
- self.assertTrue(os.path.exists('/test/file.txt'))
+ self.assertFalse(os.path.exists("/test/file.txt"))
+ example.create_file("/test/file.txt")
+ self.assertTrue(os.path.exists("/test/file.txt"))
...