Python pyfakefs: Difference between revisions

From wikinotes
No edit summary
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
mock out filesystem in memory underneath the existing python filesystem modules.
mock out filesystem in memory underneath the existing python filesystem modules.<br>
The library is slow, but very powerful, allowing you to fake macos, file-permissions, symlinks, umask etc.




= Documentation =
= Documentation =
<blockquote>
<blockquote>
{| class="wikitable"
{| class="wikitable"
|-
|-
| official docs || http://jmcgeheeiv.github.io/pyfakefs/release/index.html
| official docs || http://jmcgeheeiv.github.io/pyfakefs/release/index.html
|-
| <code>FakeFilesystem</code> apidocs || http://jmcgeheeiv.github.io/pyfakefs/release/modules.html?highlight=create_file#fake-filesystem-classes
|-
|}
|}


</blockquote><!-- documentation -->
</blockquote><!-- documentation -->
= Usage =
<blockquote>
== pytest ==
<blockquote>
The <code>pytest_pyfakefs</code> pytest module is included with <code>pyfakefs</code> and enabled automatically.
You can confirm the plugin is loaded
<syntaxhighlight lang="bash">
pytest --trace-config | less -Ri  # check under 'active_plugins:' for 'pytest_pyfakefs'
</syntaxhighlight>
The <code>FakeFilesystem</code> object is exposed as the pytest fixture <code>fs</code>.
<syntaxhighlight lang="python">
class TestMyClass:
    def test_foo(self, fs):
        fs.create_dir('/etc/foo')
        fs.create_file('/etc/foo/config.ini',
                      contents='abc\def')
</syntaxhighlight>
</blockquote><!-- pytest -->
== unittest ==
<blockquote>
</blockquote><!-- unittest -->
</blockquote><!-- Usage -->

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