This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unittest.assertLogs passes unexpectedly
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, miss-islington, mrbean-bremen, vinay.sajip
Priority: normal Keywords: patch

Created on 2020-10-05 14:39 by mrbean-bremen, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22565 merged iritkatriel, 2020-10-05 14:53
Messages (2)
msg378035 - (view) Author: (mrbean-bremen) Date: 2020-10-05 14:39
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.
msg380246 - (view) Author: miss-islington (miss-islington) Date: 2020-11-02 19:25
New changeset 6fdfcec5b11f44f27aae3d53ddeb004150ae1f61 by Irit Katriel in branch 'master':
bpo-41943: Fix bug where assertLogs doesn't correctly filter messages… (GH-22565)
https://github.com/python/cpython/commit/6fdfcec5b11f44f27aae3d53ddeb004150ae1f61
History
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86109
2021-04-16 23:13:51iritkatrielsetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.10, - Python 3.8
2020-11-02 19:25:36miss-islingtonsetnosy: + miss-islington
messages: + msg380246
2020-10-05 15:49:17vinay.sajipsetnosy: + vinay.sajip
2020-10-05 14:53:41iritkatrielsetkeywords: + patch
nosy: + iritkatriel

pull_requests: + pull_request21562
stage: patch review
2020-10-05 14:39:59mrbean-bremencreate