Skip to content

fixtures: --fixtures-per-test crashes on fixture-using doctest #15051

Description

@bluetech

Running --fixtures-per-test on pytest's own test suite crashes with:

---------------------------------------------------------------------------------------- fixtures used by test_doctest.txt -----------------------------------------------------------------------------------------
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/home/ran/src/pytest/src/_pytest/main.py", line 333, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>                          ~~~~^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/home/ran/src/pytest/src/_pytest/fixtures.py", line 2497, in _show_fixtures_per_test
INTERNALERROR>     write_item(session_item)
INTERNALERROR>     ~~~~~~~~~~^^^^^^^^^^^^^^
INTERNALERROR>   File "/home/ran/src/pytest/src/_pytest/fixtures.py", line 2491, in write_item
INTERNALERROR>     tw.sep("-", f"({get_best_relpath(item.function)})")  # type: ignore[attr-defined]
INTERNALERROR>                                      ^^^^^^^^^^^^^
INTERNALERROR> AttributeError: 'DoctestItem' object has no attribute 'function'

Doctests can use fixtures, but they don't have item.function, as the type ignore on the line correctly indicates.

Activity

  1. self-assigned this
    on Sep 17, 2026
  2. added theissue type on Sep 17, 2026
  3. added
    type: bugproblem that needs to be addressed
    topic: fixturesanything involving fixtures directly or indirectly
    on Sep 17, 2026
  4. added a commit that references this issue on Sep 17, 2026
    43ce397
  5. kilisamemarisaaa commented on Sep 19, 2026

    @kilisamemarisaaa

    I independently reproduced this on Windows before the competing fix, using pytest main 6a0de9be56365e75ff30d75cee9654739ca95f97, Python 3.12.6, and the following minimal fixture-using doctest:

    # conftest.py
    import pytest
    @pytest.fixture(autouse=True)
    def auto_fixture():
        yield
    
    # test_doctest.txt
    >>> 1 + 1
    2
    

    python -m pytest --doctest-glob="*.txt" -q test_doctest.txt passed (1 passed). The same run with --fixtures-per-test raised AttributeError: 'DoctestItem' object has no attribute 'function' in _show_fixtures_per_test and incorrectly exited 0. This confirms the report on Windows as well as Linux. I did not create a duplicate fix because #15055 already addresses the issue.

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

Metadata

Metadata

Assignees

Labels

topic: fixturesanything involving fixtures directly or indirectlytype: bugproblem that needs to be addressed

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions