Python pyfakefs

From wikinotes

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