Has anyone released a Python "mock filesystem" for automated testing?

Peter Hansen peter at engcorp.com
Thu Nov 4 21:31:04 EST 2004


The term "mock filesystem" refers to code allowing unit
or acceptance tests to create, read and write, and manipulate
in other ways "virtual" files, without any actual disk
access.  Everything is held in memory and therefore fast,
without risk of damaging real files, and with none of the
messiness of leftover files after testing.

Googling the archives and the web suggests that only I and
Remy Blank have done much along these lines.  I don't see
any sign that anyone has actually released such a beast
yet, however.

My own past work in this area was always proprietary, and
as "you can't take it with you" I've been starting over
on a new project and have the basics working.  If nobody
can point me to a more complete implementation, I'll be
happy to continue to work on mine and release it in the
near future.

For now, in case anyone is interested, I have support
for basic open, read/write, __iter__, close, "r" and "w"
modes, a couple of the most basic exceptions, and a
tiny handful of lesser things.  All well supported by
their own unit tests, of course, to ensure quality.
Stripped class/def lines shown below, as that may be
an easier way for you to picture what this is about:

class MockFile(object):
     def __init__(self, fs, path, data='', mode=''):
     def _getContents(self):
     def read(self):
     def write(self, data):
     def close(self):
     def __iter__(self):


class FileSystem(object):
     '''Acts like a real file system to support testing.'''
     def __init__(self, mocktime=False):
     def _createFile(self, path, data):
     def _getFile(self, path):
     def open(self, path, mode='r'):

Thanks for any pointers to more advanced stuff.

-Peter



More information about the Python-list mailing list