How do I test if a certain log message is logged in a Django test case?

Using the mock module for mocking the logging module or the logger object. When you’ve done that, check the arguments with which the logging function is called.

For example, if you code looks like this:

import logging

logger = logging.getLogger('my_logger')

logger.error("Your log message here")

it would look like:

from unittest.mock import patch # For python 2.x use from mock import patch

@patch('this.is.my.module.logger')
def test_check_logging_message(self, mock_logger):
    mock_logger.error.assert_called_with("Your log message here")

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)