← All notes

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

Sources

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

Permanent ID: iil:note:7h4pqd8h

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.},
}

Markdown

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.