Skip to content

Pytest does not capture teardown logs if a fixture throws an exception #12203

Open
@dcermak

Description

@dcermak

I have a pyest plugin (https://github.com/dcermak/pytest_container) that provides multiple fixtures which perform some heavy lifting (container launching). The plugin performs roughly the following action in the fixtures (simplified version, the actual implementation is here: https://github.com/dcermak/pytest_container/blob/c7f1623da7947c53768dd49f1a440a525e02948d/pytest_container/plugin.py#L67):

def fixture(request):
    with Launcher(request.param[0]) as launcher:
        try:
            launcher.start_container()
        except:
            _logger.debug("failed to start container, got log: %s", get_ctr_log())
            raise

However, pytest does not capture the log output from my plugin if an exception is actually thrown. It does capture stdout, but I would prefer to be able to use the logger for everything for the sake of consistency.

pip list

Package              Version
-------------------- ------------------------------
bci-tester           0.1.dev1205+g58f5b61.d20240410
certifi              2024.2.2
charset-normalizer   3.3.2
deprecation          2.1.0
execnet              2.1.1
filelock             3.13.4
idna                 3.6
iniconfig            2.0.0
packaging            24.0
pip                  23.2.1
pluggy               1.4.0
psycopg2             2.9.9
PyMySQL              1.1.0
pytest               8.1.1
pytest_container     0.4.2
pytest-rerunfailures 14.0
pytest-testinfra     10.1.0
pytest-xdist         3.5.0
requests             2.31.0
tenacity             8.2.3
typing_extensions    4.11.0
urllib3              2.2.1

pytest & os version

pytest 8.1.1
Fedora 39

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: loggingrelated to the logging builtin plugintopic: fixturesanything involving fixtures directly or indirectly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions