New submission from mrbean-bremen <hansemrb...@googlemail.com>:

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.

----------
components: Library (Lib)
messages: 378035
nosy: mrbean-bremen
priority: normal
severity: normal
status: open
title: unittest.assertLogs passes unexpectedly
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41943>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to