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
apidocshttp://jmcgeheeiv.github.io/pyfakefs/release/modules.html?highlight=create_file#fake-filesystem-classes
Usage
pytest
The
pytest_pyfakefs
pytest module is included withpyfakefs
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 fixturefs
.class TestMyClass: def test_foo(self, fs): fs.create_dir('/etc/foo') fs.create_file('/etc/foo/config.ini', contents='abc\def')unittest