[TIP] virtual filesystem

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Tue Dec 6 06:07:57 PST 2011

On 10:20 am, andrea.crotti.0 at gmail.com wrote:
>I have a lot of code which has to manipulate and handle the filesystem, 
>directories and so on.
>The testing now is far from complete, because even if I tried to use a 
>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 
>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 
>general, or is there a better way?

A while ago I started working on something like this.  Since `os` is 
such a fundamental part of I/O in Python, I think it's necessary to fake 
it.  From there you can proceed to something more general.

However, it's true that `os` is a sprawling mess, and it proved to be 
more effort than I cared to invest.  Hence my work is incomplete.  You 
can see it here:


in case you're curious.  If you wanted to pick it up, that'd be fine 
with me. ;)


More information about the testing-in-python mailing list