[TIP] virtual filesystem

Andrea Crotti andrea.crotti.0 at gmail.com
Tue Dec 6 02:20:06 PST 2011


I have a lot of code which has to manipulate and handle the filesystem, 
creating
directories and so on.

The testing now is far from complete, because even if I tried to use a 
functional
paradigm, still many things are too bound to the "real world".
And it's really time for me to learn how to use mock objects ;)

Before I waste too much time trying (and failing a few times), any 
suggestions of how
it could be possible?


I thought for example something like:
class FileSystemMock(object):
     """Create a filesystem like object
     """
     def __init__(self, initial_state):
         self.state = dict(initial_state) if initial_state else {}

     # what are the various things


Where I have an initial state of the directory structure, and every 
operation
will manage this dictionary of directories, raising the right errors 
when needed.

The problem is that I should rewrite the whole "os." if I want to do 
something
general, or is there a better way?



More information about the testing-in-python mailing list