小编use*_*518的帖子

Python:用于测试功能的模拟文件输入

我知道有类似的帖子,但我没有找到类似的帖子。

我在 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

unit-testing mocking python-3.x

5
推荐指数
1
解决办法
3174
查看次数

标签 统计

mocking ×1

python-3.x ×1

unit-testing ×1