Python pyfakefs: Difference between revisions

From wikinotes
No edit summary
 
Line 31: Line 31:
     def test_foo(self, fs):
     def test_foo(self, fs):
         fs.create_dir('/etc/foo')
         fs.create_dir('/etc/foo')
         fs.create_file('/etc/foo/config.ini')
         fs.create_file('/etc/foo/config.ini',
                      contents='abc\def')
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- pytest -->
</blockquote><!-- pytest -->

Latest revision as of 01:24, 23 January 2022

mock out filesystem in memory underneath the existing python filesystem modules.
The library is slow, but very powerful, allowing you to fake macos, file-permissions, symlinks, umask etc.


Documentation

official docs http://jmcgeheeiv.github.io/pyfakefs/release/index.html
FakeFilesystem apidocs http://jmcgeheeiv.github.io/pyfakefs/release/modules.html?highlight=create_file#fake-filesystem-classes

Usage

pytest

The pytest_pyfakefs pytest module is included with pyfakefs and enabled automatically.

You can confirm the plugin is loaded

pytest --trace-config | less -Ri  # check under 'active_plugins:' for 'pytest_pyfakefs'

The FakeFilesystem object is exposed as the pytest fixture fs.

class TestMyClass:
    def test_foo(self, fs):
        fs.create_dir('/etc/foo')
        fs.create_file('/etc/foo/config.ini',
                      contents='abc\def')

unittest