[Tutor] How do I test file operations (Such as opening, reading, writing, etc.)?

Alan Gauld alan.gauld at btinternet.com
Thu Jan 28 03:44:43 EST 2016


On 28/01/16 04:23, boB Stepp wrote:

> I don't want to mess with what will become the program's *real*
> classifiers.txt (And other needed text files to come, that will
> likewise be editable.), so how do I simulate these various needed file
> operations in a way that tests the actual program code, but without
> touching the actual data files?

Danny has shown you one way using a mocked filesystem.
But for your case can't you just specify a file location
as an environment variable or argv? That way you get the
advantage of using real files, which can be an important
factor in timing issues, especially if you plan on having
any concurrency going on. And it's simple to do...


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list