1
# Copyright (c) 2004 Canonical Limited
2
# Author: Robert Collins <robert.collins@canonical.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
class LogCollector(logging.Handler):
25
logging.Handler.__init__(self)
27
def emit(self, record):
28
self.records.append(record.getMessage())
30
def makeCollectingLogger():
31
"""I make a logger instance that collects its logs for programmatic analysis
32
-> (logger, collector)"""
33
logger=logging.Logger("collector")
34
handler=LogCollector()
35
handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
36
logger.addHandler(handler)
37
return logger, handler
39
class TestSuite(unittest.TestSuite):
40
"""I am an extended TestSuite with a visitor interface.
41
This is primarily to allow filtering of tests - and suites or
42
more in the future. An iterator of just tests wouldn't scale..."""
44
def visit(self, visitor):
45
"""visit the composite. Visiting is depth-first.
46
current callbacks are visitSuite and visitCase."""
47
visitor.visitSuite(self)
48
for test in self._tests:
49
#Abusing types to avoid monkey patching unittest.TestCase.
50
# Maybe that would be better?
53
except AttributeError:
54
if isinstance(test, unittest.TestCase):
55
visitor.visitCase(test)
57
print "unvisitable non-unittest.TestCase element %r" % test
59
class TestLoader(unittest.TestLoader):
60
"""Custome TestLoader to set the right TestSuite class."""
61
suiteClass = TestSuite
63
class TestVisitor(object):
64
"""A visitor for Tests"""
65
def visitSuite(self, aTestSuite):
67
def visitCase(self, aTestCase):
70
# arch-tag: b24269e1-a0bc-4024-9200-8af68ec76c02