Skip to content

The caplog fixture doesn't appear to work consistently #8977

Closed
@medley56

Description

@medley56

Taking the example test from the caplog documentation:

def test_foo(caplog):
    logging.getLogger().info("boo %s", "arg")
    print(caplog.record_tuples)
    assert caplog.record_tuples == [("root", logging.INFO, "boo arg")]

I've created a test module in the directory tests/test_test_foo.py.

When I run pytest tests, it passes (along with the rest of my tests) but when I run pytest tests/test_test_foo.py it appears that the caplog object doesn't capture anything:

❯ pytest tests/test_test_foo.py
========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 3.9.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/game1390/Workspace/Libera/libera_sdp
collected 1 item                                                                                                                                                                                         

tests/test_test_foo.py F                                                                                                                                                                           [100%]

================================================================================================ FAILURES ================================================================================================
________________________________________________________________________________________________ test_foo ________________________________________________________________________________________________

caplog = <_pytest.logging.LogCaptureFixture object at 0x10bb77310>

    def test_foo(caplog):
        logging.getLogger().info("boo %s", "arg")
>       assert caplog.record_tuples == [("root", logging.INFO, "boo arg")]
E       AssertionError: assert [] == [('root', 20, 'boo arg')]
E         Right contains one more item: ('root', 20, 'boo arg')
E         Use -v to get the full diff

tests/test_test_foo.py:6: AssertionError
======================================================================================== short test summary info =========================================================================================
FAILED tests/test_test_foo.py::test_foo - AssertionError: assert [] == [('root', 20, 'boo arg')]
=========================================================================================== 1 failed in 0.11s ============================================================================================

If anything I would have expected the opposite behavior. i.e. failing in presence of my other tests but passing on its own. Is this a bug? I don't know. I'm probably doing something wrong but I've exhausted the documentation and stackoverflow so I felt I should bring the question here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: capturerelated to the capture builtin pluginplugin: loggingrelated to the logging builtin plugintype: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions