Author mrbean-bremen
Recipients mrbean-bremen
Date 2020-10-05.14:39:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1601908800.0.0.297130696317.issue41943@roundup.psfhosted.org>
In-reply-to
Content
Related to https://bugs.python.org/issue41898, creating a new issue after the discussion with Irit Katriel on StackOverflow (https://stackoverflow.com/a/64142338/12480730).

Consider the following:

import logging
import unittest

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


class TestLogging(unittest.TestCase):
    def test_logging(self):
        with self.assertLogs(level=logging.WARNING):
            logger.info('foo')

From the docs:
The test passes if at least one message emitted inside the with block matches the logger and level conditions, otherwise it fails.

This means that this test should fail (nothing has been logged at warning level or above), but it passes. 

The reason is that in the assertLogs context manager, the log level of the passed logger is temporarily set to the passed level, and if no logging has happened on exit, the test will fail. 
If no logger is passed into the call, the temporary log level is set in the root level. If any descendant logger has it's own log level set (as in this example to INFO), this logger will still log at it's own level, and the test will pass. 

This will always happen on the condition that a descendent of the passed logger has it's own log level set that is smaller than the asserted level, and it does some logging at this level inside the assertLog context manager.
History
Date User Action Args
2020-10-05 14:40:00mrbean-bremensetrecipients: + mrbean-bremen
2020-10-05 14:40:00mrbean-bremensetmessageid: <1601908800.0.0.297130696317.issue41943@roundup.psfhosted.org>
2020-10-05 14:39:59mrbean-bremenlinkissue41943 messages
2020-10-05 14:39:59mrbean-bremencreate