如果文件在 Pytest 中不存在,如何断言?

时间:2021-05-12 10:43:50

标签: python pytest

我知道如何断言文件是否存在,但不确定如果文件不存在,最好的方法是什么。我正在使用 pytest。

到目前为止我有这个,但是如果文件确实存在,它会断言。我想也许使用 !not 但不确定标准的做法是什么。有点菜鸟。谢谢!

    path = os.path.join("cs.log")
    assert not path.is_file()   

1 个答案:

答案 0 :(得分:3)

if not os.path.exists("cs.log"):
    #stuff
    pass

或者,使用 assert

assert os.path.exists("cs.log") == False