'How can I patch / mock logging.getlogger()

I have this code that I want to test:

log = logging.getLogger(__name__)


class A(object):
    def __init__(self):
        log.debug('Init')

but I cannot figure out how to assert that log.debug was called with 'Init'

I tried patching logger but inspecting it I only found a getLogger mock.

I'm sure its simple, but I just cant figure it!

Thanks in advance for any and all help!



Solution 1:[1]

Assuming log is a global variable in a module mymod, you want to mock the actual instance that getLogger returned, which is what invokes debug. Then, you can check if log.debug was called with the correct argument.

with mock.patch('mymod.log') as log_mock:
    # test code
    log_mock.debug.assert_called_with('Init')

Solution 2:[2]

I am late for this question but another of way to achieve it is:

@patch('package_name.module_name.log')
def test_log_in_A(self, mocked_log):

    a = A()
    mocked_log.debug.assert_called_once_with('Init')

Solution 3:[3]

Here is a complete example

"""
Source to test
"""
import logging

logger = logging.getLogger("abc")

def my_fonction():
    logger.warning("Oops")

"""
Testing part
"""
import unittest
from unittest.mock import patch, MagicMock

abc_logger = logging.getLogger("abc")

class TestApp(unittest.TestCase):

    @patch.object(abc_logger, "warning", MagicMock())
    def test_my_fonction(self):
        # When
        my_fonction()
        # Then
        abc_logger.warning.assert_called_once()

Solution 4:[4]

This also works:

from unittest.mock import patch

class Test(TestCase):
    def test_logger(self):
        with patch('logging.Logger.warning') as mocked_logger:
            call_func()
            mocked_logger.assert_called_once_with('log')

Solution 5:[5]

Even simpler...

Just patch the logging module itself.

# File foo.py

import logging


log = logging.getLogger(__name__)


def main():
    log.debug('Init')



# File foo_test.py

from unittest.mock import patch

import foo


@patch.object(foo, 'logging')
def test_log_debug_called(logging):
    foo.main()
    logging.debug.assert_called_with('Init')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chepner
Solution 2 emcas88
Solution 3 Constantin De La Roche
Solution 4 sankalp
Solution 5 Chris Collett