~ubuntu-branches/ubuntu/maverick/testresources/maverick

« back to all changes in this revision

Viewing changes to lib/testresources/tests/TestUtil.py

  • Committer: Bazaar Package Importer
  • Author(s): Matej Vela
  • Date: 2006-05-19 12:58:19 UTC
  • Revision ID: james.westby@ubuntu.com-20060519125819-lt4vf6r9o2p94x1m
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

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
 
 
24
class LogCollector(logging.Handler):
 
25
    def __init__(self):
 
26
        logging.Handler.__init__(self)
 
27
        self.records=[]
 
28
    def emit(self, record):
 
29
        self.records.append(record.getMessage())
 
30
 
 
31
 
 
32
def makeCollectingLogger():
 
33
    """I make a logger instance that collects its logs for programmatic analysis
 
34
    -> (logger, collector)"""
 
35
    logger=logging.Logger("collector")
 
36
    handler=LogCollector()
 
37
    handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
 
38
    logger.addHandler(handler)
 
39
    return logger, handler
 
40
 
 
41
 
 
42
def visitTests(suite, visitor):
 
43
    """A foreign method for visiting the tests in a test suite."""
 
44
    if isinstance(suite, unittest.TestCase):
 
45
        visitor.visitCase(suite)
 
46
        return
 
47
    for test in suite._tests:
 
48
        #Abusing types to avoid monkey patching unittest.TestCase. 
 
49
        # Maybe that would be better?
 
50
        try:
 
51
            test.visit(visitor)
 
52
        except AttributeError:
 
53
            if isinstance(test, unittest.TestCase):
 
54
                visitor.visitCase(test)
 
55
            elif isinstance(test, unittest.TestSuite):
 
56
                visitor.visitSuite(test)
 
57
                visitTests(test, visitor)
 
58
            else:
 
59
                print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
 
60
    
 
61
 
 
62
class TestSuite(unittest.TestSuite):
 
63
    """I am an extended TestSuite with a visitor interface.
 
64
    This is primarily to allow filtering of tests - and suites or
 
65
    more in the future. An iterator of just tests wouldn't scale..."""
 
66
 
 
67
    def visit(self, visitor):
 
68
        """visit the composite. Visiting is depth-first.
 
69
        current callbacks are visitSuite and visitCase."""
 
70
        visitor.visitSuite(self)
 
71
        visitTests(self, visitor)
 
72
 
 
73
 
 
74
class TestLoader(unittest.TestLoader):
 
75
    """Custome TestLoader to set the right TestSuite class."""
 
76
    suiteClass = TestSuite
 
77
 
 
78
class TestVisitor(object):
 
79
    """A visitor for Tests"""
 
80
    def visitSuite(self, aTestSuite):
 
81
        pass
 
82
    def visitCase(self, aTestCase):
 
83
        pass