Message377719
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. |
|
Date |
User |
Action |
Args |
2020-09-30 19:13:37 | udalrich.schermer | set | recipients:
+ udalrich.schermer |
2020-09-30 19:13:37 | udalrich.schermer | set | messageid: <1601493217.83.0.77998878518.issue41898@roundup.psfhosted.org> |
2020-09-30 19:13:37 | udalrich.schermer | link | issue41898 messages |
2020-09-30 19:13:37 | udalrich.schermer | create | |
|