[New-bugs-announce] [issue41898] Any logging causes assertLogs to pass

Troy Daniels report at bugs.python.org
Wed Sep 30 15:13:37 EDT 2020


New submission from Troy Daniels <udalrich.schermer at gmail.com>:

The following test code unexpectedly passes.

import logging
import unittest

LOG_FORMAT = '%(levelname)-10s %(asctime)s: %(message)s'


def set_up_logger(app_name, level=logging.INFO, file="test.log"):
    formatter = logging.Formatter(LOG_FORMAT)
    log = logging.getLogger(app_name)
    # The next line lets the test pass
    log.setLevel(level)
    return log

logger = set_up_logger(__name__)

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

Based on discussion at https://stackoverflow.com/questions/64141681/any-level-of-logging-lets-assertlogs-pass, it appears that I need to pass in the logger which is going to be doing the logging.  

Doing so would make the test over-specific and fragile.  The requirement is that a warning be logged, not that a specific logger issue the warning.

This was verified with Python 3.8.5 on a Mac, but probably exists in most/all other versions.

----------
components: Tests
messages: 377719
nosy: udalrich.schermer
priority: normal
severity: normal
status: open
title: Any logging causes assertLogs to pass
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41898>
_______________________________________


More information about the New-bugs-announce mailing list