A common task in programs I've been working on lately is modifying a text file in some way. (Hey, I'm on Linux. Everything's a file. And I do large-scale system admin.)
But the file the code modifies may not exist on my desktop box. And I probably don't want to modify it if it IS on my desktop.
I've read about unit testing in Dive Into Python, and it's pretty clear what I want to do when testing an app that converts decimal to Roman Numerals (the example in DintoP). The testing is nicely self-contained. You don't need to verify that the program PRINTS the right thing, you just need to verify that the functions are returning the right output to a given input.
In my case, however, we need to test that the program is modifying its environment correctly. Here's what I've come up with:
1) Create the "original" file in a standard location, perhaps /tmp.
2) Run the function that modifies the file, passing it the path to the file in /tmp.
3) Verify that the file in /tmp was changed correctly; pass/fail unit test accordingly.
This seems kludgy to me. (Gets even kludgier if you want to verify that backup copies of the file are created properly, etc.)  Has anyone come up with a better way?
When I touch files in my code, I tend to prefer to mock the actual reading and writing of the file... so then I can give my classes exact contents I want in the test, and then assert that the test is writing back the contents I expect.
I've done this in Java, and I imagine it is quite simple in Python... but it may require designing your classes/functions in such a way that it is EASY to mock the use of an actual file.
For this, you can try passing in streams and then just pass in a simple string input/output stream which won't write to a file, or have a function that does the actual "write this string to a file" or "read this string from a file", and then replace that function in your tests.
Unit Testing File Modifications