我正在尝试测试一个函数,其中一个调用会导致写入多个文件:
def pull_files(output_files=[]):
for output_file in output_files:
content = get_content_from_server(output_file)
with open('/output/' + output_file, "wb") as code:
code.write(content)
我希望我的测试检查每个调用是否按预期打开,并且内容是否已写入:
def test_case(self):
pull_files("file1.txt", "file2.txt")
# Assert open("file1.txt", "wb") was called
# Assert "file 1 content" was written to "file1.txt"
# Assert open("file2.txt", "wb") was called
# Assert "file 2 content" was written to "file2.txt"
我在这里看到了一个处理两个文件的示例:Python mock builtin 'open' in a class using two different files
但是我不知道如何跟踪实际写入他们的内容。