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

17,656

Solution 1

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")

Solution 2

You can also use assertLogs from django.test.TestCase

When you code is

import logging

logger = logging.getLogger('my_logger')

def code_that_throws_error_log():
    logger.error("Your log message here")

This is the test code.

with self.assertLogs(logger='my_logger', level='ERROR') as cm:

    code_that_throws_error_log()

    self.assertIn(
        "ERROR:your.module:Your log message here",
        cm.output
    )

This lets you avoid patching just for logs.

Solution 3

The common way of mocking out the logger object (see the splendid chap Simeon Visser's answer) is slightly tricky in that it requires the test to mock out the logging in all the places it's done. This is awkward if the logging comes from more than one module, or is in code you don't own. If the module the logging comes from changes name, it will break your tests.

The splendid 'testfixtures' package includes tools to add a logging handler which captures all generated log messages, no matter where they come from. The captured messages can later be interrogated by the test. In its simplest form:

Assuming code-under-test, which logs:

import logging
logger = logging.getLogger()
logger.info('a message')
logger.error('an error')

A test for this would be:

from testfixtures import LogCapture
with LogCapture() as l:
    call_code_under_test()
l.check(
    ('root', 'INFO', 'a message'),
    ('root', 'ERROR', 'an error'),
)

The word 'root' indicates the logging was sent via a logger created using logging.getLogger() (i.e. with no args.) If you pass an arg to getLogger (__name__ is conventional), that arg will be used in place of 'root'.

The test does not care what module created the logging. It could be a sub-module called by our code-under-test, including 3rd party code.

The test asserts about the actual log message that was generated, as opposed to the technique of mocking, which asserts about the args that were passed. These will differ if the logging.info call uses '%s' format strings with additional arguments that you don't expand yourself (e.g. use logging.info('total=%s', len(items)) instead of logging.info('total=%s' % len(items)), which you should. It's no extra work, and allows hypothetical future logging aggregation services such as 'Sentry' to work properly - they can see that "total=12" and "total=43" are two instances of the same log message. That's the reason why pylint warns about the latter form of logging.info call.)

LogCapture includes facilities for log filtering and the like. Its parent 'testfixtures' package, written by Chris Withers, another splendid chap, includes many other useful testing tools. Documentation is here: http://pythonhosted.org/testfixtures/logging.html

Solution 4

Django has a nice context manager function called patch_logger.

from django.test.utils import patch_logger

then in your test case:

with patch_logger('logger_name', 'error') as cm:
    self.assertIn("Error message", cm)

where:

  • logger_name is the logger name (duh)
  • error is the log level
  • cm is the list of all log messages

More details:

https://github.com/django/django/blob/2.1/django/test/utils.py#L638

It should work the same for django < 2.0, independently of python version (as long as it's supported by dj)

Share:
17,656
Krystian Cybulski
Author by

Krystian Cybulski

I am also the Director of Product Engineering at 15Five. We are building the next generation of performance management and employee engagement software. I am an application programmer and web developer with over 20 years of experience. I have started building web pages when the world wide web was still at its infancy and have been involved in the industry ever since. In the past I have worked at internet startups, medium sized firms, and large financial institutions. I was with 15Five from the very beginning and saw it grow to over 190 people. I have experience in many areas, but most specifically: custom web development and specialized web applications building usable websites and online applications, concentrating on ease of use and effectiveness developing highly scalable, low-latency software for demanding industries, such as finance, banking, and live auctions I grew up, studied, and worked all my life in the United States and have returned to Poland. I am currently offering web programming and application development services to firms world-wide. To learn more about me, please have a look at my LinkedIn profile.

Updated on June 18, 2022

Comments

  • Krystian Cybulski
    Krystian Cybulski about 2 years

    I want to ensure that a certain condition in my code causes a log message to be written to the django log. How would I do this with the Django unit testing framework?

    Is there a place where I can check logged messages, similarly to how I can check sent emails? My unit test extends django.test.TestCase.