Meaning
Defines a pytest fixture that creates a temporary directory with a custom prefix using the session-scoped tmp_path_factory. Solves the problem of needing isolated, named temporary directories in test suites without manual cleanup. Reached for when the built-in tmp_path fixture does not provide enough control over directory naming or scope.
Primary Function
Test infrastructure
Communicative Purpose
Ensures tests have isolated, automatically-cleaned temporary directories with custom naming prefixes
Pattern
@pytest.fixture def fixture_name(tmp_path_factory): return tmp_path_factory.mktemp(prefix)
Core Structure
@pytest.fixture def ...(...): return ....mktemp(...)
Função primária
Test infrastructure
Propósito comunicativo
Ensures tests have isolated, automatically-cleaned temporary directories with custom naming prefixes
Situações de gatilho
Test suites: needing a custom-named temp directory fixture shared across multiple tests; Integration tests: requiring a specific directory structure under a temporary path; Plugin development: providing a reusable temp directory fixture with a meaningful prefix
Contextos
pytest, test automation, CI/CD pipelines, conftest.py files
Padrão
@pytest.fixture def fixture_name(tmp_path_factory): return tmp_path_factory.mktemp(prefix)
Estrutura central
@pytest.fixture def ...(...): return ....mktemp(...)
Slots de substituição
fixture_name: valid Python identifier for the fixture, prefix: str naming the temporary directory
Colocados típicos
- tmp_path
- tmp_path_factory
- conftest.py
- pytest.fixture
- pathlib.Path
Substituições comuns
- tmp_path fixture (built-in
- per-test directory without custom prefix
- simpler but less control)
- tmpdir fixture (legacy py.path-based
- deprecated in favor of tmp_path)
- tempfile.mkdtemp() (stdlib
- requires manual cleanup and no pytest integration)
Erros comuns
1. Using tmp_path instead of tmp_path_factory when a custom prefix is needed — tmp_path ignores custom naming, leading to generic directory names. 2. Forgetting the @pytest.fixture decorator — the function becomes a plain helper and cannot be injected into tests. 3. Returning a string instead of the pathlib.Path object — downstream code expecting Path methods like / operator will fail with AttributeError. 4. Using tmpdir_factory (legacy) instead of tmp_path_factory — returns py.path.local objects incompatible with pathlib-based code.
Similar / contraste
tmp_path fixture (per-test auto-named directory, no prefix control); tmp_path_factory.mktemp with basedir parameter (controls parent directory location); tempfile.TemporaryDirectory context manager (stdlib, requires explicit cleanup)
Interferências
Coming from unittest: may use tempfile.mkdtemp() and manual cleanup — pytest's tmp_path_factory handles cleanup automatically after the test session ends.
Família do chunk
- pytest fixtures
- tmp_path
- tmp_path_factory
- conftest.py fixtures
- test isolation
Nuance
Do not use when tmp_path alone suffices for simple per-test temp directories — tmp_path_factory.mktemp adds unnecessary complexity for the common case. The returned path is a pathlib.Path object, not a string, so str() conversion may be needed for legacy APIs. The directory is created immediately on disk; there is no lazy creation.
Efeito pragmático
Eliminates manual temporary directory cleanup code and prevents cross-test contamination by providing unique, automatically-managed directories
Dica de memória
Like renting a labeled hotel room for your test data — it's ready when you arrive, tagged with your name, and housekeeping cleans it up at checkout.
Nota
tmp_path_factory is session-scoped by default, so directories created via mktemp persist for the entire test session. For per-test isolation, prefer the tmp_path fixture instead.
Upgrade path
Session-scoped fixtures with tmp_path_factory, parametrized fixture factories, fixture scoping (scope='session')
Log in to save chunks.