pytest-dev / pytest-dev/pytest

Module level fixture log capture and association

Open
#7,819 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

There are two issues with pytest implementation of module level fixtures and logging statements therein:

  1. Any captured log is associated with the first test of the module and thus generally never printed as long as the first test of the module passes
  2. It is impossible to disable capturing of logging messages in module level fixtures via -s

Consider conftest

import logging
import pytest

@pytest.fixture(scope="module", autouse=True)
def module_fixture():
    logging.warning('LOGGING')
    print('PRINT')

and test file

import pytest

def test_pass():
    pass

def test_fail():
    pytest.fail()

We can run the pytest with default options, the module level fixtures are captured and not printed.

% python3.8 -m pytest .                                                                                                                                                         ~/test
================================================================================= test session starts =================================================================================
platform linux -- Python 3.8.0, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/ANT.AMAZON.COM/lausen/test
plugins: env-0.6.2, flaky-3.6.1
collected 2 items

test_logging.py .F                                                                                                                                                              [100%]

====================================================================================== FAILURES =======================================================================================
______________________________________________________________________________________ test_fail ______________________________________________________________________________________

    def test_fail():
>       pytest.fail()
E       Failed: <Failed instance>

test_logging.py:7: Failed
=============================================================================== short test summary info ===============================================================================
FAILED test_logging.py::test_fail - Failed: <Failed instance>
============================================================================= 1 failed, 1 passed in 0.05s =============================================================================

We can pass -s and print statement to stdout is no longer captured. The logging.warning statement is still captured and never shown:

% python3.8 -m pytest -s .                                                                                                                                                      ~/test
================================================================================= test session starts =================================================================================
platform linux -- Python 3.8.0, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/ANT.AMAZON.COM/lausen/test
plugins: env-0.6.2, flaky-3.6.1
collected 2 items

test_logging.py PRINT
.F

====================================================================================== FAILURES =======================================================================================
______________________________________________________________________________________ test_fail ______________________________________________________________________________________

    def test_fail():
>       pytest.fail()
E       Failed: <Failed instance>

test_logging.py:7: Failed
=============================================================================== short test summary info ===============================================================================
FAILED test_logging.py::test_fail - Failed: <Failed instance>
============================================================================= 1 failed, 1 passed in 0.05s =============================================================================

If we swap the order of the tests in the testfile, the captured content is shown:

% python3.8 -m pytest .                                                                                                                                                         ~/test
================================================================================= test session starts =================================================================================
platform linux -- Python 3.8.0, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/ANT.AMAZON.COM/lausen/test
plugins: env-0.6.2, flaky-3.6.1
collected 2 items

test_logging.py F.                                                                                                                                                              [100%]

====================================================================================== FAILURES =======================================================================================
______________________________________________________________________________________ test_fail ______________________________________________________________________________________

    def test_fail():
>       pytest.fail()
E       Failed: <Failed instance>

test_logging.py:4: Failed
-------------------------------------------------------------------------------- Captured stdout setup --------------------------------------------------------------------------------
PRINT
--------------------------------------------------------------------------------- Captured log setup ----------------------------------------------------------------------------------
WARNING  root:conftest.py:6 LOGGING
=============================================================================== short test summary info ===============================================================================
FAILED test_logging.py::test_fail - Failed: <Failed instance>
============================================================================= 1 failed, 1 passed in 0.06s =============================================================================

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

Reproduce the issue with the shown conftest and test file, using both default capture and -s. Start by tracing pytest's module-fixture and logging-capture handling, then add regression coverage for fixture logs and output association; done means failing-module output includes the fixture log and -s disables its capture.

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.