【问题标题】:Does tmpdir in pytest create a file only when we write to it?pytest 中的 tmpdir 是否仅在我们写入文件时才创建文件?
【发布时间】:2020-05-11 20:22:26
【问题描述】:

当我在 pytest 中使用 fp = tmpdir.join("hello.txt") 时,pytest 是否实际上在我的临时目录中创建了一个文件,或者它仅在我编写 fp.write("hello") 时创建?

【问题讨论】:

  • 你可以试试看,会发生什么。

标签: python unit-testing pytest fixtures pytest-mock


【解决方案1】:

不,pytest 在您实际写入文件之前不会创建该文件。如文档中所述,在后台,tmpdir 使用py.path。当您调用join 时,它会懒惰地获取新路径,这意味着它在您实际向其写入内容之前不存在。

【讨论】:

    猜你喜欢
    • 2021-04-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    相关资源
    最近更新 更多