mockfs makes it possible to test filesystem-dependent code by replacing functions from the os and glob modules.
import os
import unittest
import mockfs
class ExampleTestCase(unittest.TestCase):
def setUp(self):
self.mfs = mockfs.install()
def tearDown(self):
mockfs.uninstall()
def test_using_os_path(self):
self.mfs.add_entries({'/usr/bin/mockfs-magic': ''})
self.assertEqual(os.listdir('/usr/bin'), ['mockfs-magic'])
Current supported functions:
Replace builtin modules with mockfs equivalents.
| Parameter: | entries – Dictionary mapping paths to content |
|---|---|
| Rtype mockfs: | Newly installed mockfs.mfs.MockFS handler |
Example:
import mockfs
mfs = mockfs.install(entries={'/bin/ls': 'content'})