mockfs
– A simple mock filesystem for unit tests#
mockfs makes it possible to test filesystem-dependent code by
replacing functions from the os
and glob
modules.
Example Unit Test#
import os
import unittest
import mockfs
def test_context_manager():
"""The MockFS class can be used as context manager"""
with mockfs.MockFS(entries={'/tmp/does/not/exist': 'mockfs'}) as mfs:
assert os.path.exists('/tmp/does/not/exist')
with open('/tmp/does/not/exist', 'r', encoding='utf-8') as fh:
content = fh.read()
assert content == 'mockfs'
mfs.add_entries({'/tmp/does/not/exist-2': 'mockfs'})
assert os.path.exists('/tmp/does/not/exist-2')
# Context manager scope ends: everything is back to normal now.
assert not os.path.exists('/tmp/does/not/exist')
assert not os.path.exists('/tmp/does/not/exist-2')
class ExampleTestCase(unittest.TestCase):
"""The mockfs module can be used in setUp() and tearDown()"""
def setUp(self):
self.mfs = mockfs.replace_builtins()
self.mfs.add_entries({'/usr/bin/mockfs-magic': 'magic'})
def tearDown(self):
mockfs.restore_builtins()
def test_using_os_path(self):
self.assertEqual(os.listdir('/usr/bin'), ['mockfs-magic'])
def test_using_open(self):
fh = open('/usr/bin/mockfs-magic')
content = fh.read()
fh.close()
self.assertEqual(data, 'magic')
Currently supported functions: