From 4fcdaa91e96988e2754b9aec5e141e7219baae89 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 18 Jan 2013 07:27:35 -0500 Subject: [PATCH] tests/support: Factor LogCountHandler testing out into LoggedTestCase To avoid duplicating boilerplate when we need to test logged messages outside of TestPelican. --- tests/support.py | 21 +++++++++++++++++++++ tests/test_pelican.py | 19 +++++++------------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/tests/support.py b/tests/support.py index 6011e6cd..209cc665 100644 --- a/tests/support.py +++ b/tests/support.py @@ -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)) diff --git a/tests/test_pelican.py b/tests/test_pelican.py index ca0e22cc..49e20b0a 100644 --- a/tests/test_pelican.py +++ b/tests/test_pelican.py @@ -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