Search code examples
pythonunit-testingpytestfixturespytest-mock

Does tmpdir in pytest create a file only when we write to it?


When I use fp = tmpdir.join("hello.txt") in pytest, does pytest actually create a file in my temp directory or it creates only when I write fp.write("hello")?


Solution

  • No, pytest will not create the file until you actually write to it. Under the hood, as mentioned in the documentation, tmpdir uses py.path. When you call join it will lazily get the new path, meaning it does not exist until you actually write something to it.