diff options
Diffstat (limited to 'docs/autopatch.rst')
-rw-r--r-- | docs/autopatch.rst | 29 |
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")) ... |