I am trying to test that a warning is logged in a unit test. It seems that the following test should fail:
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')
However, it passes with python 3.8.5.
If I remove the log.setLevel(level)
line, the test fails, like expected. If I replace the logger.info
line with pass
, then the test also fails as expected.
Why does setting the level on the logger let the test incorrectly pass?
__name__
toMyClass.__class__.__name__
, I don't want to have to change the test. – Shower