[TIP] virtual filesystem

akira 4kir4.1i at gmail.com
Tue Dec 6 06:40:09 PST 2011


On 12/06/2011 02:20 PM, Andrea Crotti wrote:
> 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?

If https://code.google.com/p/pyfilesystem/ provides enough functionality 
for you then you could use MemoryFS instead of OSFS during tests.





More information about the testing-in-python mailing list