Skip to content

add_report_section in fixture teardown has no effect if dependent fixture failed #14162

@sh-at-cs

Description

@sh-at-cs

Short description

When a fixture fails during its setup, request.node.add_report_section calls made in the teardown sections of its (fixture) dependencies are "ignored", i.e. don't end up in the final report output printed to the terminal.

Minimal example

import pytest

@pytest.fixture
def fixture1(request):
    request.node.add_report_section('setup', 'hello', 'during setup')
    yield
    request.node.add_report_section('teardown', 'hello', 'during teardown')

@pytest.fixture
def fixture2(fixture1):
    assert False

def test_simple(fixture2):
    pass

Running this produces:

===================== ERRORS =====================
_________ ERROR at setup of test_simple __________

fixture1 = None

    @pytest.fixture
    def fixture2(fixture1):
>       assert False
E       assert False

test_foo.py:11: AssertionError
-------------- Captured hello setup --------------
during setup
============ short test summary info =============
ERROR test_foo.py::test_simple - assert False
================ 1 error in 0.04s ================

Expected behavior

There should be another section Captured hello teardown with content during teardown.

Actual behavior

See above: Only the setup report section ended up in the final report.

Additional notes

Note that the teardown section does run, as can be checked by having it write to a file or something like that. Only adding something to the report doesn't work.

Related issues

System information

  • pytest 9.0.2
  • Ubuntu 24.04
Output of pip list

I used uvx pytest so basically:

$ uvx --with pytest pip list
Package   Version
--------- -------
iniconfig 2.3.0
packaging 26.0
pip       26.0
pluggy    1.6.0
Pygments  2.19.2
pytest    9.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions