summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFree Ekanayaka <free.ekanayaka@canonical.com>2011-10-26 17:10:31 +0200
committerFree Ekanayaka <free.ekanayaka@canonical.com>2011-10-26 17:10:31 +0200
commitb44da0b0360d8a240249b6e5c007a44f53fa6cea (patch)
tree720cb7592565aef5717cdc632449659d01361eaf
parente9a67fab46df2cbb598e8bebadf03091731099b7 (diff)
downloadfixtures-b44da0b0360d8a240249b6e5c007a44f53fa6cea.tar.gz
Add LoggerFixture
-rw-r--r--lib/fixtures/__init__.py2
-rw-r--r--lib/fixtures/_fixtures/__init__.py2
-rw-r--r--lib/fixtures/_fixtures/logger.py65
-rw-r--r--lib/fixtures/tests/_fixtures/test_logger.py69
4 files changed, 138 insertions, 0 deletions
diff --git a/lib/fixtures/__init__.py b/lib/fixtures/__init__.py
index 20260d4..fbdd845 100644
--- a/lib/fixtures/__init__.py
+++ b/lib/fixtures/__init__.py
@@ -42,6 +42,7 @@ __all__ = [
'EnvironmentVariableFixture',
'Fixture',
'FunctionFixture',
+ 'LoggerFixture',
'MethodFixture',
'MonkeyPatch',
'PackagePathEntry',
@@ -56,6 +57,7 @@ __all__ = [
from fixtures.fixture import Fixture, FunctionFixture, MethodFixture
from fixtures._fixtures import (
EnvironmentVariableFixture,
+ LoggerFixture,
MonkeyPatch,
PackagePathEntry,
PopenFixture,
diff --git a/lib/fixtures/_fixtures/__init__.py b/lib/fixtures/_fixtures/__init__.py
index 6287242..082c936 100644
--- a/lib/fixtures/_fixtures/__init__.py
+++ b/lib/fixtures/_fixtures/__init__.py
@@ -18,6 +18,7 @@
__all__ = [
'EnvironmentVariableFixture',
+ 'LoggerFixture',
'MonkeyPatch',
'PackagePathEntry',
'PopenFixture',
@@ -28,6 +29,7 @@ __all__ = [
from fixtures._fixtures.environ import EnvironmentVariableFixture
+from fixtures._fixtures.logger import LoggerFixture
from fixtures._fixtures.monkeypatch import MonkeyPatch
from fixtures._fixtures.popen import PopenFixture
from fixtures._fixtures.packagepath import PackagePathEntry
diff --git a/lib/fixtures/_fixtures/logger.py b/lib/fixtures/_fixtures/logger.py
new file mode 100644
index 0000000..4664c49
--- /dev/null
+++ b/lib/fixtures/_fixtures/logger.py
@@ -0,0 +1,65 @@
+# fixtures: Fixtures with cleanups for testing and convenience.
+#
+# Copyright (c) 2010, Robert Collins <robertc@robertcollins.net>
+#
+# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
+# license at the users choice. A copy of both licenses are available in the
+# project source as Apache-2.0 and BSD. You may not use this file except in
+# compliance with one of these two licences.
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# license you chose for the specific language governing permissions and
+# limitations under that license.
+from logging import StreamHandler, getLogger, INFO, Formatter
+from cStringIO import StringIO
+
+from fixtures import Fixture
+
+__all__ = [
+ 'LoggerFixture',
+ ]
+
+
+class LoggerFixture(Fixture):
+ """Replace a logger and restore it upon cleanup.
+
+ :param name: Optionally, the name of the logger to replace.
+ :param level: Optionally, the log level to set.
+ :param format: Optionally, the format the logger should use.
+
+ Example:
+
+ def test_log(self)
+ fixture = self.useFixture(LoggerFixture())
+ logging.info('message')
+ self.assertEqual('message', fixture.output)
+ """
+
+ def __init__(self, name="", level=INFO, format=""):
+ super(LoggerFixture, self).__init__()
+ self._name = name
+ self._level = level
+ self._format = format
+
+ def setUp(self):
+ super(LoggerFixture, self).setUp()
+ self._output = StringIO()
+ self._handler = StreamHandler(self._output)
+ self._logger = getLogger(self._name)
+ self._logger.addHandler(self._handler)
+
+ if self._format:
+ self._handler.setFormatter(Formatter(self._format))
+
+ self._old_level = self._logger.level
+ if self._level:
+ self._logger.setLevel(self._level)
+
+ self.addCleanup(self._logger.removeHandler, self._handler)
+ self.addCleanup(self._logger.setLevel, self._old_level)
+
+ @property
+ def output(self):
+ return self._output.getvalue()
diff --git a/lib/fixtures/tests/_fixtures/test_logger.py b/lib/fixtures/tests/_fixtures/test_logger.py
new file mode 100644
index 0000000..f628633
--- /dev/null
+++ b/lib/fixtures/tests/_fixtures/test_logger.py
@@ -0,0 +1,69 @@
+# fixtures: Fixtures with cleanups for testing and convenience.
+#
+# Copyright (c) 2011, Robert Collins <robertc@robertcollins.net>
+#
+# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
+# license at the users choice. A copy of both licenses are available in the
+# project source as Apache-2.0 and BSD. You may not use this file except in
+# compliance with one of these two licences.
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# license you chose for the specific language governing permissions and
+# limitations under that license.
+
+import logging
+
+from testtools import TestCase
+from cStringIO import StringIO
+
+from fixtures import LoggerFixture, TestWithFixtures
+
+
+class LoggerFixtureTest(TestCase, TestWithFixtures):
+
+ def setUp(self):
+ super(LoggerFixtureTest, self).setUp()
+ # Silence the default sysout logger
+ self.handler = logging.StreamHandler(StringIO())
+ self.logger = logging.getLogger()
+ self.logger.addHandler(self.handler)
+
+ def tearDown(self):
+ super(LoggerFixtureTest, self).tearDown()
+ # Restore the default sysout logger
+ self.logger.removeHandler(self.handler)
+
+ def test_output(self):
+ """The L{LoggerFixture.output} property returns the logging output."""
+ fixture = LoggerFixture()
+ self.useFixture(fixture)
+ logging.info("some message")
+ self.assertEqual("some message\n", fixture.output)
+
+ def test_replace_and_restore_logger(self):
+ """The logger is replaced upon setup and restored upon cleanup."""
+ fixture = LoggerFixture()
+ logging.info("first message")
+ with fixture:
+ logging.info("second message")
+ logging.info("third message")
+ self.assertEqual("second message\n", fixture.output)
+
+ def test_restore_level(self):
+ """The original logging level is restored at cleanup."""
+ self.logger.setLevel(logging.DEBUG)
+ fixture = LoggerFixture(level=logging.WARNING)
+ with fixture:
+ # The fixture won't capture this, because the DEBUG level
+ # is lower than the WARNING one
+ logging.debug("debug message")
+ self.assertEqual("", fixture.output)
+
+ def test_format(self):
+ """It's possible to set an alternate format for the logger."""
+ fixture = LoggerFixture(format="%(module)s")
+ self.useFixture(fixture)
+ logging.info("message")
+ self.assertEqual("test_logger\n", fixture.output)