MCPcopy
hub / github.com/pytest-dev/pytest / tmp_path

Function tmp_path

src/_pytest/tmpdir.py:291–312  ·  view source on GitHub ↗

Return a temporary directory (as :class:`pathlib.Path` object) which is unique to each test function invocation. The temporary directory is created as a subdirectory of the base temporary directory, with configurable retention, as discussed in :ref:`temporary directory location and r

(
    request: FixtureRequest, tmp_path_factory: TempPathFactory
)

Source from the content-addressed store, hash-verified

289
290@fixture
291def tmp_path(
292 request: FixtureRequest, tmp_path_factory: TempPathFactory
293) -> Generator[Path]:
294 """Return a temporary directory (as :class:`pathlib.Path` object)
295 which is unique to each test function invocation.
296 The temporary directory is created as a subdirectory
297 of the base temporary directory, with configurable retention,
298 as discussed in :ref:`temporary directory location and retention`.
299 """
300 path = _mk_tmp(request, tmp_path_factory)
301 yield path
302
303 # Remove the tmpdir if the policy is "failed" and the test passed.
304 policy = tmp_path_factory._retention_policy
305 result_dict = request.node.stash[tmppath_result_key]
306
307 if policy == "failed" and result_dict.get("call", True):
308 # We do a "best effort" to remove files, but it might not be possible due to some leaked resource,
309 # permissions, etc, in which case we ignore it.
310 rmtree(path, ignore_errors=True)
311
312 del request.node.stash[tmppath_result_key]
313
314
315def pytest_sessionfinish(session, exitstatus: int | ExitCode):

Callers

nothing calls this directly

Calls 2

_mk_tmpFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected