February 2022
Intermediate to advanced
274 pages
6h 28m
English
The tmp_path and tmp_path_factory fixtures are used to create temporary directories. The tmp_path function-scope fixture returns a pathlib.Path instance that points to a temporary directory that sticks around during your test and a bit longer. The tmp_path_factory session-scope fixture returns a TempPathFactory object. This object has a mktemp() function that returns Path objects. You can use mktemp() to create multiple temporary directories.
You use them like this:
| | def test_tmp_path(tmp_path): |
| | file = tmp_path / "file.txt" |
| | file.write_text("Hello") |
| | assert file.read_text() == "Hello" |
| | |
| | |
| | def test_tmp_path_factory(tmp_path_factory): |
| » | path = tmp_path_factory.mktemp("sub" ... |
Read now
Unlock full access