我知道有类似的帖子,但我没有找到类似的帖子。
我在 python 中有一个函数,它接收要读取和处理的文件名作为输入并返回一些东西,我想测试我的函数的输出是否。例子:
#main function
def myfunction(filename):
f=open(filename)
for line in f:
# process data
pass
f.close()
return # something
#test function for the main function
def test_myfunction():
mockfile = #mymockfile
assert myfunction(mockfile) == #something
Run Code Online (Sandbox Code Playgroud)
我怎样才能创建一个模拟文件来测试这个函数而不必编写一个读取文件?
这是我发现的最接近模拟我需要的东西(http://www.voidspace.org.uk/python/mock/helpers.html#mock-open)