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

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:

mockfs.install(entries=None, pathmap=None)

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'})
mockfs.uninstall()
Restore the original builtin functions.

Indices and tables

Table Of Contents

Next topic

os.path and glob Implementations

This Page