1
0
Fork 0
forked from github/pelican

tests/support: Factor LogCountHandler testing out into LoggedTestCase

To avoid duplicating boilerplate when we need to test logged messages
outside of TestPelican.
This commit is contained in:
W. Trevor King 2013-01-18 07:27:35 -05:00
commit 4fcdaa91e9
2 changed files with 28 additions and 12 deletions

View file

@ -176,3 +176,24 @@ class LogCountHandler(BufferingHandler):
if (msg is None or re.match(msg, l.getMessage()))
and (level is None or l.levelno == level)
])
class LoggedTestCase(unittest.TestCase):
"""A test case that captures log messages
"""
def setUp(self):
super(LoggedTestCase, self).setUp()
self._logcount_handler = LogCountHandler()
logging.getLogger().addHandler(self._logcount_handler)
def tearDown(self):
logging.getLogger().removeHandler(self._logcount_handler)
super(LoggedTestCase, self).tearDown()
def assertLogCountEqual(self, count=None, msg=None, **kwargs):
actual = self._logcount_handler.count_logs(msg=msg, **kwargs)
self.assertEqual(
actual, count,
msg='expected {} occurrences of {!r}, but found {}'.format(
count, msg, actual))