~ubuntu-branches/ubuntu/quantal/config-manager/quantal

« back to all changes in this revision

Viewing changes to tests/TestUtil.py

  • Committer: Bazaar Package Importer
  • Author(s): Anand Kumria
  • Date: 2005-10-24 13:01:11 UTC
  • mto: (3.1.1 dapper)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20051024130111-3muotu4me5hhhiow
Tags: upstream-0.2
ImportĀ upstreamĀ versionĀ 0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2004 Canonical Limited
 
2
#       Author: Robert Collins <robert.collins@canonical.com>
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
#
 
18
 
 
19
import sys
 
20
import logging
 
21
import unittest
 
22
 
 
23
class LogCollector(logging.Handler):
 
24
    def __init__(self):
 
25
        logging.Handler.__init__(self)
 
26
        self.records=[]
 
27
    def emit(self, record):
 
28
        self.records.append(record.getMessage())
 
29
 
 
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
 
38
 
 
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..."""
 
43
 
 
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?
 
51
            try:
 
52
                test.visit(visitor)
 
53
            except AttributeError:
 
54
                if isinstance(test, unittest.TestCase):
 
55
                    visitor.visitCase(test)
 
56
                else:
 
57
                    print "unvisitable non-unittest.TestCase element %r" % test
 
58
 
 
59
class TestLoader(unittest.TestLoader):
 
60
    """Custome TestLoader to set the right TestSuite class."""
 
61
    suiteClass = TestSuite
 
62
 
 
63
class TestVisitor(object):
 
64
    """A visitor for Tests"""
 
65
    def visitSuite(self, aTestSuite):
 
66
        pass
 
67
    def visitCase(self, aTestCase):
 
68
        pass
 
69
 
 
70
# arch-tag: b24269e1-a0bc-4024-9200-8af68ec76c02