pytest and the tmp_path fixture
Today I learned that you don’t need to import temp file or directory generators, nor create them yourself, when you’re testing a code with pytest
.
All you have to do is use the tmp_path
fixture, and pytest
will take care of everything. Example:
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
assert 0
I learned this while writing unit tests for the findlike tool.