我问了这个问题,关于如何写一个pytest来检查输出stdout并得到一个解决方案.现在我需要写一个test case,检查内容是否写入文件,内容是否按预期写入,例如:
def writetoafile():
file = open("output.txt",w)
file.write("hello\n")
file.write("world\n")
file.close()
Run Code Online (Sandbox Code Playgroud)
现在是一个pytest函数来检查它是否写入:
def test_writeToFile():
file = open("ouput.txt",'r')
expected = "hello\nworld\n"
assert expected==file.read()
Run Code Online (Sandbox Code Playgroud)
虽然这似乎有效,但我认为这不是理想的,尤其是硬编码.这些test functions写入文件通常是如何编写的?