serhii.net

In the middle of the desert you can say anything you want

17 May 2022

pytest temporary files

Pytest has a nice tmp_path fixture that creates a temporary directory and returs the Path1:

# content of test_tmp_path.py
CONTENT = "content"


def test_create_file(tmp_path):
   d = tmp_path / "sub"
   d.mkdir()
   p = d / "hello.txt"
   p.write_text(CONTENT)
   assert p.read_text() == CONTENT
   assert len(list(tmp_path.iterdir())) == 1
Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus