#πŸ”’ Pytesting with logging module (caplog)

4 messages Β· Page 1 of 1 (latest)

vital solstice
#

this works when i run the test individually
however when running all my test, these 4 always fail
I have tried

  • no logger
  • caplog.set_level(..., logger="test_registry")
    none worked when running all tests, but worked when tested individually
@pytest.fixture
def registry(caplog):
    # Create a new logger for the test
    logger = logging.getLogger("test_registry")
    logger.setLevel(logging.DEBUG)

    # Make sure the logger propagates to the root logger where caplog is listening
    logger.propagate = True

    # Create registry with the logger
    registry = Registry(logger=logger)
    yield registry
    registry.reset()

def test_registry_logging_register(registry, caplog):
    caplog.set_level(logging.DEBUG)

    registry.register('argInt', 1)
    assert f"{registry.registry_name}: Registered `argInt` to registry" in caplog.text

    with pytest.raises(DuplicateKeyError):
        registry.register('argInt', 2)

    assert f"{registry.registry_name}: `argInt` already registered." in caplog.text

Error

tests\test_registryManager\test_registryManager_logging.py:63 (test_registry_logging_get)
'BaseRegistry: `notExistingKey` not registered.' != ''

Expected :''
Actual   :'BaseRegistry: `notExistingKey` not registered.'
<Click to see difference>

registry = <Registry name='BaseRegistry', items=0>
caplog = <_pytest.logging.LogCaptureFixture object at 0x00000240F2B299A0>

    def test_registry_logging_get(registry, caplog):
        caplog.set_level(logging.DEBUG)
        with pytest.raises(MissingKeyError):
            registry.get('notExistingKey')
    
>       assert f"{registry.registry_name}: `notExistingKey` not registered." in caplog.text
E       AssertionError: assert 'BaseRegistry: `notExistingKey` not registered.' in ''
E        +  where '' = <_pytest.logging.LogCaptureFixture object at 0x00000240F2B299A0>.text
eager oxideBOT
#

@vital solstice

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

eager oxideBOT
#

@vital solstice

Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.