The tmp_path fixture gives each test function its own temporary directory as a pathlib.Path
Answer: The tmp_path fixture will provide a temporary directory unique to each test function, and tmp_path is a pathlib.Path object. Here is an example test usage.
tmp_path_factory and the legacy tmpdir fixtures
tmp_path_factory is session-scoped and can create extra temporary directories from another fixture or test. If the suite needs a large procedurally generated image, build that image once per session to save time.
The older tmpdir and tmpdir_factory fixtures are similar, but they return legacy py.path.local objects rather than pathlib.Path. These days it is preferred to use tmp_path and tmp_path_factory. By default pytest retains the temporary directory for the last three invocations.
See also
Claims
- id: C1 claim: tmp_path provides a temporary directory unique to each test function. source: S1 evidence: "temporary directory unique to each test function"
- id: C2 claim: tmp_path is a pathlib.Path object. source: S1 evidence: "tmp_path is a pathlib.Path object"
- id: C3 claim: tmp_path_factory is a session-scoped fixture for creating temporary directories. source: S2 evidence: "tmp_path_factory`` is a session-scoped fixture"
- id: C4 claim: The tmp_path fixture gives each test function a unique temporary directory. source: S2 evidence: "temporary directory unique to each test function"
Sources
- [S1] How to use temporary directories and files in tests. pytest docs. https://docs.pytest.org/en/stable/how-to/tmp_path.html . License: MIT. Accessed 2026-11-17.
- [S2] tmp_path.rst (pytest source docs). pytest-dev/pytest (GitHub). https://github.com/pytest-dev/pytest/blob/main/doc/en/how-to/tmp_path.rst . License: MIT. Accessed 2026-11-17.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: pytest docs, How to use temporary directories and files in tests, MIT (https://raw.githubusercontent.com/pytest-dev/pytest/main/LICENSE); pytest-dev/pytest (GitHub), tmp_path.rst (pytest source docs), MIT (https://raw.githubusercontent.com/pytest-dev/pytest/main/LICENSE). Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: pytest docs, How to use temporary directories and files in tests, MIT (https://raw.githubusercontent.com/pytest-dev/pytest/main/LICENSE); pytest-dev/pytest (GitHub), tmp_path.rst (pytest source docs), MIT (https://raw.githubusercontent.com/pytest-dev/pytest/main/LICENSE).
Cite this
iinventedlight. 2026. "The tmp_path fixture gives each test function its own temporary directory as a pathlib.Path." iinventedlight Library, iil:note:7h4pqd8h, version of 2026-11-17. https://iinventedlight.com/library/notes/pytest-tmp-path/. License: CC-BY-4.0.
@misc{iil_note_7h4pqd8h,
author = {{iinventedlight}},
title = {{The tmp\_path fixture gives each test function its own temporary directory as a pathlib.Path}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-17},
url = {https://iinventedlight.com/library/notes/pytest-tmp-path/},
note = {Permanent ID iil:note:7h4pqd8h. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/pytest-tmp-path.md.