~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/TestUtil.py

(vila) Open 2.4.3 for bug fixes (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
#
18
18
 
19
19
import sys
20
20
import logging
21
21
import unittest
22
22
 
 
23
from bzrlib import pyutils
 
24
 
23
25
# Mark this python module as being part of the implementation
24
26
# of unittest: this gives us better tracebacks where the last
25
27
# shown frame is the test code, not our assertXYZ.
27
29
 
28
30
 
29
31
class LogCollector(logging.Handler):
 
32
 
30
33
    def __init__(self):
31
34
        logging.Handler.__init__(self)
32
35
        self.records=[]
 
36
 
33
37
    def emit(self, record):
34
38
        self.records.append(record.getMessage())
35
39
 
58
62
                visitor.visitSuite(test)
59
63
                visitTests(test, visitor)
60
64
            else:
61
 
                print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
 
65
                print "unvisitable non-unittest.TestCase element %r (%r)" % (
 
66
                    test, test.__class__)
62
67
 
63
68
 
64
69
class TestSuite(unittest.TestSuite):
72
77
        visitor.visitSuite(self)
73
78
        visitTests(self, visitor)
74
79
 
 
80
    def run(self, result):
 
81
        """Run the tests in the suite, discarding references after running."""
 
82
        tests = list(self)
 
83
        tests.reverse()
 
84
        self._tests = []
 
85
        while tests:
 
86
            if result.shouldStop:
 
87
                self._tests = reversed(tests)
 
88
                break
 
89
            tests.pop().run(result)
 
90
        return result
 
91
 
75
92
 
76
93
class TestLoader(unittest.TestLoader):
77
94
    """Custom TestLoader to extend the stock python one."""
94
111
 
95
112
    def loadTestsFromModuleName(self, name):
96
113
        result = self.suiteClass()
97
 
        module = _load_module_by_name(name)
 
114
        module = pyutils.get_named_object(name)
98
115
 
99
116
        result.addTests(self.loadTestsFromModule(module))
100
117
        return result
123
140
        >>>         result.addTests([test, test])
124
141
        >>>     return result
125
142
        """
126
 
        basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
143
        if sys.version_info < (2, 7):
 
144
            basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
145
        else:
 
146
            # GZ 2010-07-19: Python 2.7 unittest also uses load_tests but with
 
147
            #                a different and incompatible signature
 
148
            basic_tests = super(TestLoader, self).loadTestsFromModule(module,
 
149
                use_load_tests=False)
127
150
        load_tests = getattr(module, "load_tests", None)
128
151
        if load_tests is not None:
129
152
            return load_tests(basic_tests, module, self)
161
184
            return self.suiteClass()
162
185
 
163
186
 
164
 
def _load_module_by_name(mod_name):
165
 
    parts = mod_name.split('.')
166
 
    module = __import__(mod_name)
167
 
    del parts[0]
168
 
    # for historical reasons python returns the top-level module even though
169
 
    # it loads the submodule; we need to walk down to get the one we want.
170
 
    while parts:
171
 
        module = getattr(module, parts.pop(0))
172
 
    return module
173
 
 
174
 
 
175
187
class TestVisitor(object):
176
188
    """A visitor for Tests"""
 
189
 
177
190
    def visitSuite(self, aTestSuite):
178
191
        pass
 
192
 
179
193
    def visitCase(self, aTestCase):
180
194
        pass