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:
parent
13cd0a4cb3
commit
4fcdaa91e9
2 changed files with 28 additions and 12 deletions
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -1,9 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals, print_function
|
||||
try:
|
||||
import unittest2 as unittest
|
||||
except ImportError:
|
||||
import unittest # NOQA
|
||||
|
||||
import os
|
||||
from filecmp import dircmp
|
||||
|
|
@ -14,7 +10,7 @@ import logging
|
|||
|
||||
from pelican import Pelican
|
||||
from pelican.settings import read_settings
|
||||
from .support import LogCountHandler
|
||||
from .support import LoggedTestCase
|
||||
|
||||
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
SAMPLES_PATH = os.path.abspath(os.sep.join((CURRENT_DIR, "..", "samples")))
|
||||
|
|
@ -39,13 +35,12 @@ def recursiveDiff(dcmp):
|
|||
return diff
|
||||
|
||||
|
||||
class TestPelican(unittest.TestCase):
|
||||
class TestPelican(LoggedTestCase):
|
||||
# general functional testing for pelican. Basically, this test case tries
|
||||
# to run pelican in different situations and see how it behaves
|
||||
|
||||
def setUp(self):
|
||||
self.logcount_handler = LogCountHandler()
|
||||
logging.getLogger().addHandler(self.logcount_handler)
|
||||
super(TestPelican, self).setUp()
|
||||
self.temp_path = mkdtemp()
|
||||
self.old_locale = locale.setlocale(locale.LC_ALL)
|
||||
locale.setlocale(locale.LC_ALL, str('C'))
|
||||
|
|
@ -53,7 +48,7 @@ class TestPelican(unittest.TestCase):
|
|||
def tearDown(self):
|
||||
rmtree(self.temp_path)
|
||||
locale.setlocale(locale.LC_ALL, self.old_locale)
|
||||
logging.getLogger().removeHandler(self.logcount_handler)
|
||||
super(TestPelican, self).tearDown()
|
||||
|
||||
def assertFilesEqual(self, diff):
|
||||
msg = "some generated files differ from the expected functional " \
|
||||
|
|
@ -79,10 +74,10 @@ class TestPelican(unittest.TestCase):
|
|||
pelican.run()
|
||||
dcmp = dircmp(self.temp_path, os.sep.join((OUTPUT_PATH, "basic")))
|
||||
self.assertFilesEqual(recursiveDiff(dcmp))
|
||||
self.assertEqual(self.logcount_handler.count_logs(
|
||||
self.assertLogCountEqual(
|
||||
count=10,
|
||||
msg="Unable to find.*skipping url replacement",
|
||||
level=logging.WARNING,
|
||||
), 10, msg="bad number of occurences found for this log")
|
||||
level=logging.WARNING)
|
||||
|
||||
def test_custom_generation_works(self):
|
||||
# the same thing with a specified set of settings should work
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue