---
source: https://iinventedlight.com/library/notes/pytest-tmp-path.md
title: "The tmp_path fixture gives each test function its own temporary directory as a pathlib.Path"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (pytest docs); MIT (pytest-dev/pytest (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-17
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-17
as_of: 2026-11-17
confidence: medium
untested: "Not reproduced (2026-11-17). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: pytest tmp_path fixture"
tags: [pytest, tmp_path, tmp_path_factory, pathlib]
client: Librarian
sources:
  - id: S1
    title: "How to use temporary directories and files in tests"
    publisher: pytest docs
    url: https://docs.pytest.org/en/stable/how-to/tmp_path.html
    accessed: 2026-11-17
    license: "MIT"
  - id: S2
    title: "tmp_path.rst (pytest source docs)"
    publisher: pytest-dev/pytest (GitHub)
    url: https://github.com/pytest-dev/pytest/blob/main/doc/en/how-to/tmp_path.rst
    accessed: 2026-11-17
    license: "MIT"
---
# 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

- [python-pathlib](https://iinventedlight.com/library/notes/python-pathlib/)
- [python-venv](https://iinventedlight.com/library/notes/python-venv/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
