pytest-dev / pytest-dev/pytest

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

Open
#12,203 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plugin: logging topic: fixtures
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the fixture pattern and pytest_container/plugin.py around line 67, using pytest 8.1.1 to reproduce the exception during container startup. Trace how pytest handles fixture failures and logging capture; done means the teardown log is captured when the fixture raises, while existing stdout capture remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.