tmp_path gives each test a fresh directory under a per-user base, and pytest deliberately retains the directories from the three most recent runs before removing older ones. This is useful for inspecting artefacts after a failure and surprising when a suite writes large files, because disk usage grows to three full runs.
The base directory is printed in the header when -q is not in effect, and --basetemp=DIR overrides it. Note that pytest empties --basetemp at the start of the run, so pointing it at a directory containing anything you care about deletes that content.
tmp_path_factory.mktemp("name") is the session-scoped equivalent, needed because tmp_path itself is function-scoped and cannot be requested from a wider fixture. The retention count is configurable through tmp_path_retention_count and tmp_path_retention_policy in the ini file, where the policy can be set to failed to keep directories only for tests that failed. In CI, setting the policy to none avoids filling the runner's disk.