[TIP] Tesatble file type

Michael Foord fuzzyman at voidspace.org.uk
Thu Oct 15 09:48:15 PDT 2009


Hello all,

I thought this list might be interested in this.

For my online Python tutorial Try Python (http://www.trypython.org/ ) I 
reimplemented the file type and the open function to use browser local 
storage as you don't access to the filesystem using Silverlight.

I did a blog entry about the new file type:

    http://www.voidspace.org.uk/python/weblog/arch_d7_2009_10_10.shtml#e1129

As well as the local storage backend it has a dictionary based backend 
(the default backend in fact) and functions replace_builtins and 
restore_builtins. replace_builtins (unsurprisingly) replaces the builtin 
file type and open functions with the ones in storage.py - making them 
potentially useful for testing.

File access can be difficult to test without either using real access 
(slow and possibly littering files around or brittle) or changing your 
production code to use dependency injection or a similar technique to 
allow your tests to swap out the file system layer.

 From the blog entry:

Using this implementation you can swap out the builtin file type during 
the test, controlling how it behaves using the dictionary backend, 
without having to change the way you write your production code just to 
make it testable.

For this to be really useful it needs an implementation of functions in 
the os and os.path module (like os.listdir, os.remove, os.makedir(s), 
os.path.isfile, os.path.isdir and so on). This should be easy to do and 
I will get round to these would be nice things to cover in the Try 
Python tutorial.

Useful or a really bad idea?

Michael

-- 
http://www.ironpythoninaction.com/




More information about the testing-in-python mailing list