Unit testing should usually take into account things such as streams and the file system. Do you have a reason for unit test with actual file system, user input, etc.?
Python makes a monkey patch very easy; you can, for example, replace the entire os / sys module with a mock object (e.g. Python Mock ) so that you never have to deal with the file system. It will also make your tests faster.
, , , , . , .. .
Edit
, "" .
, , my_module, get_text_upper:
def get_text_upper(filename):
return open(filename).read().upper()
( , , ...). open, StringIO:
from cStringIO import StringIO
def fake_open(text):
fp = StringIO()
fp.write(text)
fp.seek(0)
return fp
def test_get_text():
my_module.open = lambda *args, **kwargs : fake_open("foo")
text = my_module.get_text_upper("foo.txt")
assert text == "FOO", text
.
fooobar.com/questions/64246/....