When developing unit tests, you may find the TestHandler and Matcher classes useful.
Typical usage:
import logging
from logutils.testing import TestHandler, Matcher
import unittest
class LoggingTest(unittest.TestCase):
def setUp(self):
self.handler = h = TestHandler(Matcher())
self.logger = l = logging.getLogger()
l.addHandler(h)
def tearDown(self):
self.logger.removeHandler(self.handler)
self.handler.close()
def test_simple(self):
"Simple test of logging test harness."
# Just as a demo, let's log some messages.
# Only one should show up in the log.
self.logger.debug("This won't show up.")
self.logger.info("Neither will this.")
self.logger.warning("But this will.")
h = self.handler
self.assertTrue(h.matches(levelno=logging.WARNING))
self.assertFalse(h.matches(levelno=logging.DEBUG))
self.assertFalse(h.matches(levelno=logging.INFO))
def test_partial(self):
"Test of partial matching in logging test harness."
# Just as a demo, let's log some messages.
# Only one should show up in the log.
self.logger.debug("This won't show up.")
self.logger.info("Neither will this.")
self.logger.warning("But this will.")
h = self.handler
self.assertTrue(h.matches(msg="ut th")) # from "But this will"
self.assertTrue(h.matches(message="ut th")) # from "But this will"
self.assertFalse(h.matches(message="either"))
self.assertFalse(h.matches(message="won't"))
def test_multiple(self):
"Test of matching multiple values in logging test harness."
# Just as a demo, let's log some messages.
# Only one should show up in the log.
self.logger.debug("This won't show up.")
self.logger.info("Neither will this.")
self.logger.warning("But this will.")
self.logger.error("And so will this.")
h = self.handler
self.assertTrue(h.matches(levelno=logging.WARNING,
message='ut thi'))
self.assertTrue(h.matches(levelno=logging.ERROR,
message='nd so wi'))
self.assertFalse(h.matches(levelno=logging.INFO))
This utility class matches a stored dictionary of logging.LogRecord attributes with keyword arguments passed to its matches() method.
Try to match a single stored value (dv) with a supplied value (v).
Return True if found, else False.
Parameters: |
|
---|
Try to match a single dict with the supplied arguments.
Keys whose values are strings and which are in self._partial_matches will be checked for partial (i.e. substring) matches. You can extend this scheme to (for example) do regular expression matching, etc.
Return True if found, else False.
Parameters: | kwargs – A set of keyword arguments whose names are LogRecord attributes and whose values are what you want to match in a stored LogRecord. |
---|
This handler collects records in a buffer for later inspection by your unit test code.
Parameters: | matcher – The Matcher instance to use for matching. |
---|
The number of records in the buffer.
Saves the __dict__ of the record in the buffer attribute, and the formatted records in the formatted attribute.
Parameters: | record – The record to emit. |
---|
Clears out the buffer and formatted attributes.
Accept a list of keyword argument values and ensure that the handler’s buffer of stored records matches the list one-for-one.
Return True if exactly matched, else False.
Parameters: | kwarglist – A list of keyword-argument dictionaries, each of which will be passed to matches() with the corresponding record from the buffer. |
---|
Look for a saved dict whose keys/values match the supplied arguments.
Return True if found, else False.
Parameters: | kwargs – A set of keyword arguments whose names are LogRecord attributes and whose values are what you want to match in a stored LogRecord. |
---|
Should the buffer be flushed?
This returns False - you’ll need to flush manually, usually after your unit test code checks the buffer contents against your expectations.