相关疑难解决方法(0)

编写pytest函数来检查输出到python中的文件?

我问了这个问题,关于如何写一个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写入文件通常是如何编写的?

python file-io function pytest

15
推荐指数
2
解决办法
9070
查看次数

标签 统计

file-io ×1

function ×1

pytest ×1

python ×1