~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/TestUtil.py

  • Committer: Aaron Bentley
  • Date: 2005-09-29 21:07:17 UTC
  • mfrom: (1393.1.6)
  • mto: (1185.25.1)
  • mto: This revision was merged to the branch mainline in revision 1419.
  • Revision ID: abentley@panoramicfeedback.com-20050929210717-cd73981590f17017
Merged the weave changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (c) 2004 Canonical Limited
2
2
#       Author: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
#
18
18
 
19
19
import sys
20
20
import logging
21
21
import unittest
22
22
 
23
 
# Mark this python module as being part of the implementation
24
 
# of unittest: this gives us better tracebacks where the last
25
 
# shown frame is the test code, not our assertXYZ.
26
 
__unittest = 1
27
 
 
28
23
 
29
24
class LogCollector(logging.Handler):
30
25
    def __init__(self):
47
42
def visitTests(suite, visitor):
48
43
    """A foreign method for visiting the tests in a test suite."""
49
44
    for test in suite._tests:
50
 
        #Abusing types to avoid monkey patching unittest.TestCase.
 
45
        #Abusing types to avoid monkey patching unittest.TestCase. 
51
46
        # Maybe that would be better?
52
47
        try:
53
48
            test.visit(visitor)
59
54
                visitTests(test, visitor)
60
55
            else:
61
56
                print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
62
 
 
 
57
    
63
58
 
64
59
class TestSuite(unittest.TestSuite):
65
60
    """I am an extended TestSuite with a visitor interface.
72
67
        visitor.visitSuite(self)
73
68
        visitTests(self, visitor)
74
69
 
75
 
    def run(self, result):
76
 
        """Run the tests in the suite, discarding references after running."""
77
 
        tests = list(self)
78
 
        tests.reverse()
79
 
        self._tests = []
80
 
        while tests:
81
 
            if result.shouldStop:
82
 
                self._tests = reversed(tests)
83
 
                break
84
 
            tests.pop().run(result)
85
 
        return result
86
 
 
87
70
 
88
71
class TestLoader(unittest.TestLoader):
89
 
    """Custom TestLoader to extend the stock python one."""
90
 
 
 
72
    """Custome TestLoader to set the right TestSuite class."""
91
73
    suiteClass = TestSuite
92
 
    # Memoize test names by test class dict
93
 
    test_func_names = {}
94
 
 
95
 
    def loadTestsFromModuleNames(self, names):
96
 
        """use a custom means to load tests from modules.
97
 
 
98
 
        There is an undesirable glitch in the python TestLoader where a
99
 
        import error is ignore. We think this can be solved by ensuring the
100
 
        requested name is resolvable, if its not raising the original error.
101
 
        """
102
 
        result = self.suiteClass()
103
 
        for name in names:
104
 
            result.addTests(self.loadTestsFromModuleName(name))
105
 
        return result
106
 
 
107
 
    def loadTestsFromModuleName(self, name):
108
 
        result = self.suiteClass()
109
 
        module = _load_module_by_name(name)
110
 
 
111
 
        result.addTests(self.loadTestsFromModule(module))
112
 
        return result
113
 
 
114
 
    def loadTestsFromModule(self, module):
115
 
        """Load tests from a module object.
116
 
 
117
 
        This extension of the python test loader looks for an attribute
118
 
        load_tests in the module object, and if not found falls back to the
119
 
        regular python loadTestsFromModule.
120
 
 
121
 
        If a load_tests attribute is found, it is called and the result is
122
 
        returned.
123
 
 
124
 
        load_tests should be defined like so:
125
 
        >>> def load_tests(standard_tests, module, loader):
126
 
        >>>    pass
127
 
 
128
 
        standard_tests is the tests found by the stock TestLoader in the
129
 
        module, module and loader are the module and loader instances.
130
 
 
131
 
        For instance, to run every test twice, you might do:
132
 
        >>> def load_tests(standard_tests, module, loader):
133
 
        >>>     result = loader.suiteClass()
134
 
        >>>     for test in iter_suite_tests(standard_tests):
135
 
        >>>         result.addTests([test, test])
136
 
        >>>     return result
137
 
        """
138
 
        if sys.version_info < (2, 7):
139
 
            basic_tests = super(TestLoader, self).loadTestsFromModule(module)
140
 
        else:
141
 
            # GZ 2010-07-19: Python 2.7 unittest also uses load_tests but with
142
 
            #                a different and incompatible signature
143
 
            basic_tests = super(TestLoader, self).loadTestsFromModule(module,
144
 
                use_load_tests=False)
145
 
        load_tests = getattr(module, "load_tests", None)
146
 
        if load_tests is not None:
147
 
            return load_tests(basic_tests, module, self)
148
 
        else:
149
 
            return basic_tests
150
 
 
151
 
    def getTestCaseNames(self, test_case_class):
152
 
        test_fn_names = self.test_func_names.get(test_case_class, None)
153
 
        if test_fn_names is not None:
154
 
            # We already know them
155
 
            return test_fn_names
156
 
 
157
 
        test_fn_names = unittest.TestLoader.getTestCaseNames(self,
158
 
                                                             test_case_class)
159
 
        self.test_func_names[test_case_class] = test_fn_names
160
 
        return test_fn_names
161
 
 
162
 
 
163
 
class FilteredByModuleTestLoader(TestLoader):
164
 
    """A test loader that import only the needed modules."""
165
 
 
166
 
    def __init__(self, needs_module):
167
 
        """Constructor.
168
 
 
169
 
        :param needs_module: a callable taking a module name as a
170
 
            parameter returing True if the module should be loaded.
171
 
        """
172
 
        TestLoader.__init__(self)
173
 
        self.needs_module = needs_module
174
 
 
175
 
    def loadTestsFromModuleName(self, name):
176
 
        if self.needs_module(name):
177
 
            return TestLoader.loadTestsFromModuleName(self, name)
178
 
        else:
179
 
            return self.suiteClass()
180
 
 
181
 
 
182
 
def _load_module_by_name(mod_name):
183
 
    parts = mod_name.split('.')
184
 
    module = __import__(mod_name)
185
 
    del parts[0]
186
 
    # for historical reasons python returns the top-level module even though
187
 
    # it loads the submodule; we need to walk down to get the one we want.
188
 
    while parts:
189
 
        module = getattr(module, parts.pop(0))
190
 
    return module
191
 
 
192
74
 
193
75
class TestVisitor(object):
194
76
    """A visitor for Tests"""