Skip to content

Retain tmp paths for setup and teardown errors - #14999

Open
GruffElixir wants to merge 4 commits into
pytest-dev:mainfrom
GruffElixir:fix/14998-retain-tmp-path-on-error
Open

Retain tmp paths for setup and teardown errors#14999
GruffElixir wants to merge 4 commits into
pytest-dev:mainfrom
GruffElixir:fix/14998-retain-tmp-path-on-error

Conversation

@GruffElixir

Copy link
Copy Markdown

Fixes #14998.

tmp_path_retention_policy = "failed" currently removes a temporary directory before setup or teardown outcomes are known. This loses debugging evidence for fixture setup errors and teardown errors.

This change defers the retention decision until the teardown report is available. Setup skips keep the existing cleanup behavior from #10502, while setup errors, call failures, and teardown errors retain their directories.

Tests:

  • uv run --frozen pytest testing/test_tmpdir.py -q (49 passed, 11 skipped)
  • uvx --from ruff ruff check src/_pytest/tmpdir.py testing/test_tmpdir.py
  • uvx --from ruff ruff format --check src/_pytest/tmpdir.py testing/test_tmpdir.py
  • Full suite: 4450 passed, 157 skipped, 14 xfailed, 1 xpassed; one unrelated failure in testing/acceptance_test.py::TestInvocationVariants::test_pydoc due to Windows/Python 3.14 UTF-8 decoding.
  • git diff --check

Copilot AI lite review requested due to automatic review settings September 10, 2026 13:28
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Str0k

Str0k commented Sep 12, 2026

Copy link
Copy Markdown

I found a regression in the setup-skip path: removing tmp_path from
pytest_runtest_makereport can run before dependent fixtures finish teardown.
A fixture that accesses its temporary files after yield then raises
FileNotFoundError, turning a skipped test into a teardown error.

Minimal reproduction, run with -o tmp_path_retention_policy=failed:

import pytest


@pytest.fixture
def resource(tmp_path):
    (tmp_path / "input.txt").write_text("fixture data", encoding="utf-8")
    yield tmp_path
    assert (tmp_path / "input.txt").read_text(encoding="utf-8") == "fixture data"


@pytest.fixture
def unavailable(resource):
    pytest.skip("optional service unavailable")


def test_optional_service(unavailable):
    pass

The base commit 3fd8675 reports 1 skipped; PR head ca34872 reports
1 skipped, 1 error on Python 3.12. Could cleanup for setup skips be deferred
until dependent fixture teardown completes? This would also make a useful
regression test alongside the setup/teardown error cases.

Disclosure: Codex assisted with identifying and reproducing this case.

@GruffElixir

Copy link
Copy Markdown
Author

Thanks for the repro. I reproduced the setup-skip teardown case and pushed 890f4ce to this PR. Cleanup now waits for the final teardown report, so dependent fixtures can still use tmp_path; teardown errors still keep the directory. The tmpdir suite is 50 passed, 11 skipped, and Ruff is clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tmp_path_retention_policy="failed" removes directories for setup and teardown errors

3 participants