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
)
| 289 | |
| 290 | @fixture |
| 291 | def 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 | |
| 315 | def pytest_sessionfinish(session, exitstatus: int | ExitCode): |