How can I patch / mock logging.getlogger()

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

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

def test_log_in_A(self, mocked_log):

    a = A()

This also works:

from unittest.mock import patch

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

You can use patch.object() on the actual logging object. that lets you verify that you're using the correct logger too:

logger = logging.getLogger('')
with mock.patch.object(logger, 'debug') as mock_debug:

Alternatively, if you're using Pytest, then it already has a fixture that captures logs for you:

def test_bar(caplog):
    with caplog.at_level(logging.DEBUG):
    assert "Init" in caplog.text
    # or, if you really need to check the log-level
    assert caplog.records[-1].message == "Init"
    assert caplog.records[-1].levelname == "DEBUG"

More info in the pytest docs on logging